Skip to content

Commit

Permalink
Merge pull request #1988 from ghaerr/prectimer
Browse files Browse the repository at this point in the history
[debug] Use same C file for C library and kernel precision timer routines
  • Loading branch information
ghaerr authored Sep 3, 2024
2 parents c753481 + afeb6de commit ef3fba0
Showing 1 changed file with 57 additions and 6 deletions.
63 changes: 57 additions & 6 deletions elks/arch/i86/lib/prectimer.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/*
* Precision timer routines for IBM PC and compatibles
* 2 Aug 2024 Greg Haerr
*
* Use 8254 PIT to measure elapsed time in pticks = 0.8381 usecs.
*/

#include <linuxmt/prectimer.h>
#include <linuxmt/sched.h>
#include <linuxmt/kernel.h>
Expand All @@ -6,12 +13,23 @@
#include <arch/irq.h>
#include <arch/io.h>

/*
* Precision timer routines for IBM PC and compatibles
* 2 Aug 2024 Greg Haerr
*
* Use 8254 PIT to measure elapsed time in pticks = 0.8381 usecs.
*/
#ifndef __KERNEL__
#include <linuxmt/mem.h>
#include <linuxmt/memory.h>
#include <sys/linksym.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define printk printf

/* FIXME values for IBM PC only, included for non-ifdef'd libc multi-arch compilation */
#ifndef TIMER_CMDS_PORT
#define TIMER_CMDS_PORT 0x43
#define TIMER_DATA_PORT 0x40
#endif

#endif

/*
* Each ptick corresponds to the elapsed time for a countdown in the 8254 PIT.
Expand All @@ -31,6 +49,31 @@

static unsigned int lastjiffies; /* only 16 bits required within ~10.9 mins */

#ifndef __KERNEL__
static unsigned short __far *pjiffies; /* only access low order jiffies word */

#define errmsg(str) write(STDERR_FILENO, str, sizeof(str) - 1)

void init_ptime(void)
{
int fd, offset, kds;

__LINK_SYMBOL(ptostr);
fd = open("/dev/kmem", O_RDONLY);
if (fd < 0) {
errmsg("No kmem\n");
return;
}
if (ioctl(fd, MEM_GETDS, &kds) < 0 ||
ioctl(fd, MEM_GETJIFFADDR, &offset) < 0) {
errmsg("No mem ioctl\n");
} else {
pjiffies = _MK_FP(kds, offset);
}
close(fd);
}
#endif

/*
* Each PIT count (ptick) is 0.8381 usecs each for 10ms jiffies timer (= 1/11932)
*
Expand All @@ -53,8 +96,13 @@ unsigned long get_ptime(void)
clr_irq(); /* synchronize countdown and jiffies */
outb(0, TIMER_CMDS_PORT); /* latch timer value */
/* 16-bit subtract handles low word wrap automatically */
#ifndef __KERNEL__
jdiff = *pjiffies - (unsigned)lastjiffies;
lastjiffies = *pjiffies; /* 16 bit save works for ~10.9 mins */
#else
jdiff = (unsigned)jiffies - (unsigned)lastjiffies;
lastjiffies = (unsigned)jiffies; /* 16 bit save works for ~10.9 mins */
#endif
lo = inb(TIMER_DATA_PORT);
hi = inb(TIMER_DATA_PORT) << 8;
restore_flags(flags);
Expand All @@ -73,6 +121,8 @@ unsigned long get_ptime(void)
}

#if TIMER_TEST

#ifdef __KERNEL__
void test_ptime_idle_loop(void)
{
static int v;
Expand All @@ -84,6 +134,7 @@ void test_ptime_idle_loop(void)
while (jiffies < timeout)
;
}
#endif

void test_ptime_print(void)
{
Expand Down

0 comments on commit ef3fba0

Please sign in to comment.