Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[debug] Use same C file for C library and kernel precision timer routines #1988

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading