History log of /openbsd-current/sys/arch/i386/isa/clock.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.70 08-Jun-2024 jsg

remove unused SECMIN and SECHOUR defines


# 1.69 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_4_BASE OPENBSD_7_5_BASE
# 1.68 17-Sep-2023 cheloha

clockintr: remove clockintr_init(), clockintr_flags

All the state initialization once done in clockintr_init() has been
moved to other parts of the kernel. It's a dead function. Remove it.

Likewise, the clockintr_flags variable no longer sports any meaningful
flags. Remove it. This frees up the CL_* flag namespace, which might
be useful to the clockintr frontend if we ever need to add behavior
flags to any of those functions.


# 1.67 23-Aug-2023 cheloha

all platforms: separate cpu_initclocks() from cpu_startclock()

To give the primary CPU an opportunity to perform clock interrupt
preparation in a machine-independent manner we need to separate the
"initialization" parts of cpu_initclocks() from the "start the clock
interrupt" parts. Currently, cpu_initclocks() does everything all at
once, so there is no space for this MI setup.

Many platforms have more-or-less already done this separation by
implementing a separate routine named "cpu_startclock()". This patch
promotes cpu_startclock() from de facto standard to mandatory API.

- Prototype cpu_startclock() in sys/systm.h alongside cpu_initclocks().
The separation of responsibility between the two routines is a bit
fuzzy but the basic guidelines are as follows:

+ cpu_initclocks() must initialize hz, stathz, and profhz, and call
clockintr_init().

+ cpu_startclock() must call clockintr_cpu_init() and start the clock
interrupt cycle on the calling CPU.

These guidelines will shift in the future, but that's the way things
stand as of *this* commit.

- In initclocks(): first call cpu_initclocks(), then do MI setup, and
last call cpu_startclock().

- On platforms where cpu_startclock() already exists: don't call
cpu_startclock() from cpu_initclocks() anymore.

- On platforms where cpu_startclock() doesn't yet exist: implement it.
Usually this is as simple as dividing cpu_initclocks() in two.

Tested on amd64 (i8254, lapic), arm64, i386 (i8254, lapic), macppc,
mips64/octeon, and sparc64. Tested on arm/armv7 (agtimer(4)) by
phessler@ and jmatthew@. Tested on m88k/luna88k by aoyama@. Tested
on powerpc64 by gkoehler@ and mlarkin@. Tested on riscv64 by
jmatthew@.

Thread: https://marc.info/?l=openbsd-tech&m=169195251322149&w=2


# 1.66 22-Aug-2023 cheloha

i386: i8254_initclocks: set IPL_MPSAFE for clock/rtc IRQs

Setting IPL_MPSAFE for the i8254/mc146818 IRQs appeases a KASSERT in
apic_intr_establish() and allows the system to boot via the i8254
path. This makes testing changes to the i8254/mc146818 code much
easier on modern hardware without mucking with the GENERIC config. We
already set IPL_MPSAFE for these IRQs in the equivalent amd64 code.

Now, setting IPL_MPSAFE is a lie: the i8254 and mc146818 IRQs are not
MP-safe. However, the lie is harmless because we only reach
i8254_initclocks() if (a) there is no APIC at all, or (b) we fail to
calibrate the local APIC timer.

Thread: https://marc.info/?l=openbsd-tech&m=169258915227321&w=2

ok mlarkin@


# 1.65 25-Jul-2023 cheloha

statclock: move profil(2), GPROF code to profclock(), gmonclock()

This patch isolates profil(2) and GPROF from statclock(). Currently,
statclock() implements both profil(2) and GPROF through a complex
mechanism involving both platform code (setstatclockrate) and the
scheduler (pscnt, psdiv, and psratio). We have a machine-independent
interface to the clock interrupt hardware now, so we no longer need to
do it this way.

- Move profil(2)-specific code from statclock() to a new clock
interrupt callback, profclock(), in subr_prof.c. Each
schedstate_percpu has its own profclock handle. The profclock is
enabled/disabled for a given CPU when it is needed by the running
thread during mi_switch() and sched_exit().

- Move GPROF-specific code from statclock() to a new clock interrupt
callback, gmonclock(), in subr_prof.c. Where available, each cpu_info
has its own gmonclock handle . The gmonclock is enabled/disabled for
a given CPU via sysctl(2) in prof_state_toggle().

- Both profclock() and gmonclock() have a fixed period, profclock_period,
that is initialized during initclocks().

- Export clockintr_advance(), clockintr_cancel(), clockintr_establish(),
and clockintr_stagger() via <sys/clockintr.h>. They have external
callers now.

- Delete pscnt, psdiv, psratio. From schedstate_percpu, also delete
spc_pscnt and spc_psdiv. The statclock frequency is not dynamic
anymore so these variables are now useless.

- Delete code/state related to the dynamic statclock frequency from
kern_clockintr.c. The statclock frequency can still be pseudo-random,
so move the contents of clockintr_statvar_init() into clockintr_init().

With input from miod@, deraadt@, and claudio@. Early revisions
cleaned up by claudio. Early revisions tested by claudio@. Tested by
cheloha@ on amd64, arm64, macppc, octeon, and sparc64 (sun4v).
Compile- and boot- tested on i386 by mlarkin@. riscv64 compilation
bugs found by mlarkin@. Tested on riscv64 by jca@. Tested on
powerpc64 by gkoehler@.


Revision tags: OPENBSD_7_3_BASE
# 1.64 04-Feb-2023 cheloha

timecounting: remove incomplete PPS support

The timecounting code has had stubs for pulse-per-second (PPS) polling
since it was imported in 2004. At this point it seems unlikely that
anyone is going to finish adding PPS support, so let's remove the stubs:

- Delete the dead tc_poll_pps() call from tc_windup().
- Remove all tc_poll_pps symbols from the kernel.

Link: https://marc.info/?l=openbsd-tech&m=167519035723210&w=2

ok miod@


# 1.63 30-Jan-2023 jsg

remove unneeded includes in arch/i386
ok krw@ miod@


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.69 13-May-2024 jsg

remove prototypes with no matching function
ok mpi@


Revision tags: OPENBSD_7_4_BASE OPENBSD_7_5_BASE
# 1.68 17-Sep-2023 cheloha

clockintr: remove clockintr_init(), clockintr_flags

All the state initialization once done in clockintr_init() has been
moved to other parts of the kernel. It's a dead function. Remove it.

Likewise, the clockintr_flags variable no longer sports any meaningful
flags. Remove it. This frees up the CL_* flag namespace, which might
be useful to the clockintr frontend if we ever need to add behavior
flags to any of those functions.


# 1.67 23-Aug-2023 cheloha

all platforms: separate cpu_initclocks() from cpu_startclock()

To give the primary CPU an opportunity to perform clock interrupt
preparation in a machine-independent manner we need to separate the
"initialization" parts of cpu_initclocks() from the "start the clock
interrupt" parts. Currently, cpu_initclocks() does everything all at
once, so there is no space for this MI setup.

Many platforms have more-or-less already done this separation by
implementing a separate routine named "cpu_startclock()". This patch
promotes cpu_startclock() from de facto standard to mandatory API.

- Prototype cpu_startclock() in sys/systm.h alongside cpu_initclocks().
The separation of responsibility between the two routines is a bit
fuzzy but the basic guidelines are as follows:

+ cpu_initclocks() must initialize hz, stathz, and profhz, and call
clockintr_init().

+ cpu_startclock() must call clockintr_cpu_init() and start the clock
interrupt cycle on the calling CPU.

These guidelines will shift in the future, but that's the way things
stand as of *this* commit.

- In initclocks(): first call cpu_initclocks(), then do MI setup, and
last call cpu_startclock().

- On platforms where cpu_startclock() already exists: don't call
cpu_startclock() from cpu_initclocks() anymore.

- On platforms where cpu_startclock() doesn't yet exist: implement it.
Usually this is as simple as dividing cpu_initclocks() in two.

Tested on amd64 (i8254, lapic), arm64, i386 (i8254, lapic), macppc,
mips64/octeon, and sparc64. Tested on arm/armv7 (agtimer(4)) by
phessler@ and jmatthew@. Tested on m88k/luna88k by aoyama@. Tested
on powerpc64 by gkoehler@ and mlarkin@. Tested on riscv64 by
jmatthew@.

Thread: https://marc.info/?l=openbsd-tech&m=169195251322149&w=2


# 1.66 22-Aug-2023 cheloha

i386: i8254_initclocks: set IPL_MPSAFE for clock/rtc IRQs

Setting IPL_MPSAFE for the i8254/mc146818 IRQs appeases a KASSERT in
apic_intr_establish() and allows the system to boot via the i8254
path. This makes testing changes to the i8254/mc146818 code much
easier on modern hardware without mucking with the GENERIC config. We
already set IPL_MPSAFE for these IRQs in the equivalent amd64 code.

Now, setting IPL_MPSAFE is a lie: the i8254 and mc146818 IRQs are not
MP-safe. However, the lie is harmless because we only reach
i8254_initclocks() if (a) there is no APIC at all, or (b) we fail to
calibrate the local APIC timer.

Thread: https://marc.info/?l=openbsd-tech&m=169258915227321&w=2

ok mlarkin@


# 1.65 25-Jul-2023 cheloha

statclock: move profil(2), GPROF code to profclock(), gmonclock()

This patch isolates profil(2) and GPROF from statclock(). Currently,
statclock() implements both profil(2) and GPROF through a complex
mechanism involving both platform code (setstatclockrate) and the
scheduler (pscnt, psdiv, and psratio). We have a machine-independent
interface to the clock interrupt hardware now, so we no longer need to
do it this way.

- Move profil(2)-specific code from statclock() to a new clock
interrupt callback, profclock(), in subr_prof.c. Each
schedstate_percpu has its own profclock handle. The profclock is
enabled/disabled for a given CPU when it is needed by the running
thread during mi_switch() and sched_exit().

- Move GPROF-specific code from statclock() to a new clock interrupt
callback, gmonclock(), in subr_prof.c. Where available, each cpu_info
has its own gmonclock handle . The gmonclock is enabled/disabled for
a given CPU via sysctl(2) in prof_state_toggle().

- Both profclock() and gmonclock() have a fixed period, profclock_period,
that is initialized during initclocks().

- Export clockintr_advance(), clockintr_cancel(), clockintr_establish(),
and clockintr_stagger() via <sys/clockintr.h>. They have external
callers now.

- Delete pscnt, psdiv, psratio. From schedstate_percpu, also delete
spc_pscnt and spc_psdiv. The statclock frequency is not dynamic
anymore so these variables are now useless.

- Delete code/state related to the dynamic statclock frequency from
kern_clockintr.c. The statclock frequency can still be pseudo-random,
so move the contents of clockintr_statvar_init() into clockintr_init().

With input from miod@, deraadt@, and claudio@. Early revisions
cleaned up by claudio. Early revisions tested by claudio@. Tested by
cheloha@ on amd64, arm64, macppc, octeon, and sparc64 (sun4v).
Compile- and boot- tested on i386 by mlarkin@. riscv64 compilation
bugs found by mlarkin@. Tested on riscv64 by jca@. Tested on
powerpc64 by gkoehler@.


Revision tags: OPENBSD_7_3_BASE
# 1.64 04-Feb-2023 cheloha

timecounting: remove incomplete PPS support

The timecounting code has had stubs for pulse-per-second (PPS) polling
since it was imported in 2004. At this point it seems unlikely that
anyone is going to finish adding PPS support, so let's remove the stubs:

- Delete the dead tc_poll_pps() call from tc_windup().
- Remove all tc_poll_pps symbols from the kernel.

Link: https://marc.info/?l=openbsd-tech&m=167519035723210&w=2

ok miod@


# 1.63 30-Jan-2023 jsg

remove unneeded includes in arch/i386
ok krw@ miod@


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.68 17-Sep-2023 cheloha

clockintr: remove clockintr_init(), clockintr_flags

All the state initialization once done in clockintr_init() has been
moved to other parts of the kernel. It's a dead function. Remove it.

Likewise, the clockintr_flags variable no longer sports any meaningful
flags. Remove it. This frees up the CL_* flag namespace, which might
be useful to the clockintr frontend if we ever need to add behavior
flags to any of those functions.


# 1.67 23-Aug-2023 cheloha

all platforms: separate cpu_initclocks() from cpu_startclock()

To give the primary CPU an opportunity to perform clock interrupt
preparation in a machine-independent manner we need to separate the
"initialization" parts of cpu_initclocks() from the "start the clock
interrupt" parts. Currently, cpu_initclocks() does everything all at
once, so there is no space for this MI setup.

Many platforms have more-or-less already done this separation by
implementing a separate routine named "cpu_startclock()". This patch
promotes cpu_startclock() from de facto standard to mandatory API.

- Prototype cpu_startclock() in sys/systm.h alongside cpu_initclocks().
The separation of responsibility between the two routines is a bit
fuzzy but the basic guidelines are as follows:

+ cpu_initclocks() must initialize hz, stathz, and profhz, and call
clockintr_init().

+ cpu_startclock() must call clockintr_cpu_init() and start the clock
interrupt cycle on the calling CPU.

These guidelines will shift in the future, but that's the way things
stand as of *this* commit.

- In initclocks(): first call cpu_initclocks(), then do MI setup, and
last call cpu_startclock().

- On platforms where cpu_startclock() already exists: don't call
cpu_startclock() from cpu_initclocks() anymore.

- On platforms where cpu_startclock() doesn't yet exist: implement it.
Usually this is as simple as dividing cpu_initclocks() in two.

Tested on amd64 (i8254, lapic), arm64, i386 (i8254, lapic), macppc,
mips64/octeon, and sparc64. Tested on arm/armv7 (agtimer(4)) by
phessler@ and jmatthew@. Tested on m88k/luna88k by aoyama@. Tested
on powerpc64 by gkoehler@ and mlarkin@. Tested on riscv64 by
jmatthew@.

Thread: https://marc.info/?l=openbsd-tech&m=169195251322149&w=2


# 1.66 22-Aug-2023 cheloha

i386: i8254_initclocks: set IPL_MPSAFE for clock/rtc IRQs

Setting IPL_MPSAFE for the i8254/mc146818 IRQs appeases a KASSERT in
apic_intr_establish() and allows the system to boot via the i8254
path. This makes testing changes to the i8254/mc146818 code much
easier on modern hardware without mucking with the GENERIC config. We
already set IPL_MPSAFE for these IRQs in the equivalent amd64 code.

Now, setting IPL_MPSAFE is a lie: the i8254 and mc146818 IRQs are not
MP-safe. However, the lie is harmless because we only reach
i8254_initclocks() if (a) there is no APIC at all, or (b) we fail to
calibrate the local APIC timer.

Thread: https://marc.info/?l=openbsd-tech&m=169258915227321&w=2

ok mlarkin@


# 1.65 25-Jul-2023 cheloha

statclock: move profil(2), GPROF code to profclock(), gmonclock()

This patch isolates profil(2) and GPROF from statclock(). Currently,
statclock() implements both profil(2) and GPROF through a complex
mechanism involving both platform code (setstatclockrate) and the
scheduler (pscnt, psdiv, and psratio). We have a machine-independent
interface to the clock interrupt hardware now, so we no longer need to
do it this way.

- Move profil(2)-specific code from statclock() to a new clock
interrupt callback, profclock(), in subr_prof.c. Each
schedstate_percpu has its own profclock handle. The profclock is
enabled/disabled for a given CPU when it is needed by the running
thread during mi_switch() and sched_exit().

- Move GPROF-specific code from statclock() to a new clock interrupt
callback, gmonclock(), in subr_prof.c. Where available, each cpu_info
has its own gmonclock handle . The gmonclock is enabled/disabled for
a given CPU via sysctl(2) in prof_state_toggle().

- Both profclock() and gmonclock() have a fixed period, profclock_period,
that is initialized during initclocks().

- Export clockintr_advance(), clockintr_cancel(), clockintr_establish(),
and clockintr_stagger() via <sys/clockintr.h>. They have external
callers now.

- Delete pscnt, psdiv, psratio. From schedstate_percpu, also delete
spc_pscnt and spc_psdiv. The statclock frequency is not dynamic
anymore so these variables are now useless.

- Delete code/state related to the dynamic statclock frequency from
kern_clockintr.c. The statclock frequency can still be pseudo-random,
so move the contents of clockintr_statvar_init() into clockintr_init().

With input from miod@, deraadt@, and claudio@. Early revisions
cleaned up by claudio. Early revisions tested by claudio@. Tested by
cheloha@ on amd64, arm64, macppc, octeon, and sparc64 (sun4v).
Compile- and boot- tested on i386 by mlarkin@. riscv64 compilation
bugs found by mlarkin@. Tested on riscv64 by jca@. Tested on
powerpc64 by gkoehler@.


Revision tags: OPENBSD_7_3_BASE
# 1.64 04-Feb-2023 cheloha

timecounting: remove incomplete PPS support

The timecounting code has had stubs for pulse-per-second (PPS) polling
since it was imported in 2004. At this point it seems unlikely that
anyone is going to finish adding PPS support, so let's remove the stubs:

- Delete the dead tc_poll_pps() call from tc_windup().
- Remove all tc_poll_pps symbols from the kernel.

Link: https://marc.info/?l=openbsd-tech&m=167519035723210&w=2

ok miod@


# 1.63 30-Jan-2023 jsg

remove unneeded includes in arch/i386
ok krw@ miod@


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.67 23-Aug-2023 cheloha

all platforms: separate cpu_initclocks() from cpu_startclock()

To give the primary CPU an opportunity to perform clock interrupt
preparation in a machine-independent manner we need to separate the
"initialization" parts of cpu_initclocks() from the "start the clock
interrupt" parts. Currently, cpu_initclocks() does everything all at
once, so there is no space for this MI setup.

Many platforms have more-or-less already done this separation by
implementing a separate routine named "cpu_startclock()". This patch
promotes cpu_startclock() from de facto standard to mandatory API.

- Prototype cpu_startclock() in sys/systm.h alongside cpu_initclocks().
The separation of responsibility between the two routines is a bit
fuzzy but the basic guidelines are as follows:

+ cpu_initclocks() must initialize hz, stathz, and profhz, and call
clockintr_init().

+ cpu_startclock() must call clockintr_cpu_init() and start the clock
interrupt cycle on the calling CPU.

These guidelines will shift in the future, but that's the way things
stand as of *this* commit.

- In initclocks(): first call cpu_initclocks(), then do MI setup, and
last call cpu_startclock().

- On platforms where cpu_startclock() already exists: don't call
cpu_startclock() from cpu_initclocks() anymore.

- On platforms where cpu_startclock() doesn't yet exist: implement it.
Usually this is as simple as dividing cpu_initclocks() in two.

Tested on amd64 (i8254, lapic), arm64, i386 (i8254, lapic), macppc,
mips64/octeon, and sparc64. Tested on arm/armv7 (agtimer(4)) by
phessler@ and jmatthew@. Tested on m88k/luna88k by aoyama@. Tested
on powerpc64 by gkoehler@ and mlarkin@. Tested on riscv64 by
jmatthew@.

Thread: https://marc.info/?l=openbsd-tech&m=169195251322149&w=2


# 1.66 22-Aug-2023 cheloha

i386: i8254_initclocks: set IPL_MPSAFE for clock/rtc IRQs

Setting IPL_MPSAFE for the i8254/mc146818 IRQs appeases a KASSERT in
apic_intr_establish() and allows the system to boot via the i8254
path. This makes testing changes to the i8254/mc146818 code much
easier on modern hardware without mucking with the GENERIC config. We
already set IPL_MPSAFE for these IRQs in the equivalent amd64 code.

Now, setting IPL_MPSAFE is a lie: the i8254 and mc146818 IRQs are not
MP-safe. However, the lie is harmless because we only reach
i8254_initclocks() if (a) there is no APIC at all, or (b) we fail to
calibrate the local APIC timer.

Thread: https://marc.info/?l=openbsd-tech&m=169258915227321&w=2

ok mlarkin@


# 1.65 25-Jul-2023 cheloha

statclock: move profil(2), GPROF code to profclock(), gmonclock()

This patch isolates profil(2) and GPROF from statclock(). Currently,
statclock() implements both profil(2) and GPROF through a complex
mechanism involving both platform code (setstatclockrate) and the
scheduler (pscnt, psdiv, and psratio). We have a machine-independent
interface to the clock interrupt hardware now, so we no longer need to
do it this way.

- Move profil(2)-specific code from statclock() to a new clock
interrupt callback, profclock(), in subr_prof.c. Each
schedstate_percpu has its own profclock handle. The profclock is
enabled/disabled for a given CPU when it is needed by the running
thread during mi_switch() and sched_exit().

- Move GPROF-specific code from statclock() to a new clock interrupt
callback, gmonclock(), in subr_prof.c. Where available, each cpu_info
has its own gmonclock handle . The gmonclock is enabled/disabled for
a given CPU via sysctl(2) in prof_state_toggle().

- Both profclock() and gmonclock() have a fixed period, profclock_period,
that is initialized during initclocks().

- Export clockintr_advance(), clockintr_cancel(), clockintr_establish(),
and clockintr_stagger() via <sys/clockintr.h>. They have external
callers now.

- Delete pscnt, psdiv, psratio. From schedstate_percpu, also delete
spc_pscnt and spc_psdiv. The statclock frequency is not dynamic
anymore so these variables are now useless.

- Delete code/state related to the dynamic statclock frequency from
kern_clockintr.c. The statclock frequency can still be pseudo-random,
so move the contents of clockintr_statvar_init() into clockintr_init().

With input from miod@, deraadt@, and claudio@. Early revisions
cleaned up by claudio. Early revisions tested by claudio@. Tested by
cheloha@ on amd64, arm64, macppc, octeon, and sparc64 (sun4v).
Compile- and boot- tested on i386 by mlarkin@. riscv64 compilation
bugs found by mlarkin@. Tested on riscv64 by jca@. Tested on
powerpc64 by gkoehler@.


Revision tags: OPENBSD_7_3_BASE
# 1.64 04-Feb-2023 cheloha

timecounting: remove incomplete PPS support

The timecounting code has had stubs for pulse-per-second (PPS) polling
since it was imported in 2004. At this point it seems unlikely that
anyone is going to finish adding PPS support, so let's remove the stubs:

- Delete the dead tc_poll_pps() call from tc_windup().
- Remove all tc_poll_pps symbols from the kernel.

Link: https://marc.info/?l=openbsd-tech&m=167519035723210&w=2

ok miod@


# 1.63 30-Jan-2023 jsg

remove unneeded includes in arch/i386
ok krw@ miod@


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.65 25-Jul-2023 cheloha

statclock: move profil(2), GPROF code to profclock(), gmonclock()

This patch isolates profil(2) and GPROF from statclock(). Currently,
statclock() implements both profil(2) and GPROF through a complex
mechanism involving both platform code (setstatclockrate) and the
scheduler (pscnt, psdiv, and psratio). We have a machine-independent
interface to the clock interrupt hardware now, so we no longer need to
do it this way.

- Move profil(2)-specific code from statclock() to a new clock
interrupt callback, profclock(), in subr_prof.c. Each
schedstate_percpu has its own profclock handle. The profclock is
enabled/disabled for a given CPU when it is needed by the running
thread during mi_switch() and sched_exit().

- Move GPROF-specific code from statclock() to a new clock interrupt
callback, gmonclock(), in subr_prof.c. Where available, each cpu_info
has its own gmonclock handle . The gmonclock is enabled/disabled for
a given CPU via sysctl(2) in prof_state_toggle().

- Both profclock() and gmonclock() have a fixed period, profclock_period,
that is initialized during initclocks().

- Export clockintr_advance(), clockintr_cancel(), clockintr_establish(),
and clockintr_stagger() via <sys/clockintr.h>. They have external
callers now.

- Delete pscnt, psdiv, psratio. From schedstate_percpu, also delete
spc_pscnt and spc_psdiv. The statclock frequency is not dynamic
anymore so these variables are now useless.

- Delete code/state related to the dynamic statclock frequency from
kern_clockintr.c. The statclock frequency can still be pseudo-random,
so move the contents of clockintr_statvar_init() into clockintr_init().

With input from miod@, deraadt@, and claudio@. Early revisions
cleaned up by claudio. Early revisions tested by claudio@. Tested by
cheloha@ on amd64, arm64, macppc, octeon, and sparc64 (sun4v).
Compile- and boot- tested on i386 by mlarkin@. riscv64 compilation
bugs found by mlarkin@. Tested on riscv64 by jca@. Tested on
powerpc64 by gkoehler@.


Revision tags: OPENBSD_7_3_BASE
# 1.64 04-Feb-2023 cheloha

timecounting: remove incomplete PPS support

The timecounting code has had stubs for pulse-per-second (PPS) polling
since it was imported in 2004. At this point it seems unlikely that
anyone is going to finish adding PPS support, so let's remove the stubs:

- Delete the dead tc_poll_pps() call from tc_windup().
- Remove all tc_poll_pps symbols from the kernel.

Link: https://marc.info/?l=openbsd-tech&m=167519035723210&w=2

ok miod@


# 1.63 30-Jan-2023 jsg

remove unneeded includes in arch/i386
ok krw@ miod@


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.64 04-Feb-2023 cheloha

timecounting: remove incomplete PPS support

The timecounting code has had stubs for pulse-per-second (PPS) polling
since it was imported in 2004. At this point it seems unlikely that
anyone is going to finish adding PPS support, so let's remove the stubs:

- Delete the dead tc_poll_pps() call from tc_windup().
- Remove all tc_poll_pps symbols from the kernel.

Link: https://marc.info/?l=openbsd-tech&m=167519035723210&w=2

ok miod@


# 1.63 30-Jan-2023 jsg

remove unneeded includes in arch/i386
ok krw@ miod@


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.63 30-Jan-2023 jsg

remove unneeded includes in arch/i386
ok krw@ miod@


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.62 06-Dec-2022 cheloha

i386: switch to clockintr

In lapic timer mode:

- Rip out lapic_delay(). We can't use the lapic timer to delay(9) when
it's running in one-shot mode.
- Add a randomized statclock(), stathz = hz.
- Add profiling support, profhz = stathz * 10.
- Wire up lapic_intrclock.

In i8254-mode:

- i8254's clockintr() does not have a monopoly on hardclock(9).
- mc146818's rtcintr() does not have a monopoly on statclock().
- In profiling mode, the statclock() will drift very slightly
because (profhz = 1024) does not divide into 1 billion. Need to
consider how best to fix this.

ACPI suspend/resume tested by mlarkin@ via ESXi. Tons of testing by
Scott Bennett on a Pentium 4 machine; APM suspend/resume confirmed to
work there, too.

Link: https://marc.info/?l=openbsd-tech&m=166776370803446&w=2

ok mlarkin@


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.61 01-Nov-2022 kettenis

Use todr_attach().

ok phessler@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE OPENBSD_7_1_BASE OPENBSD_7_2_BASE
# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.60 23-Feb-2021 cheloha

timecounting: use C99-style initialization for all timecounter structs

The timecounter struct is large and I think it may change in the
future. Changing it later will be easier if we use C99-style
initialization for all timecounter structs. It also makes reading the
code a bit easier.

For reasons I cannot explain, switching to C99-style initialization
sometimes changes the hash of the resulting object file, even though
the resulting struct should be the same. So there is a binary change
here, but only sometimes. No behavior should change in either case.

I can't compile-test this everywhere but I have been staring at the
diff for days now and I'm relatively confident this will not break
compilation. Fingers crossed.

ok gnezdo@


Revision tags: OPENBSD_6_8_BASE
# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.59 06-Jul-2020 pirofti

Add support for timeconting in userland.

This diff exposes parts of clock_gettime(2) and gettimeofday(2) to
userland via libc eliberating processes from the need for a context
switch everytime they want to count the passage of time.

If a timecounter clock can be exposed to userland than it needs to set
its tc_user member to a non-zero value. Tested with one or multiple
counters per architecture.

The timing data is shared through a pointer found in the new ELF
auxiliary vector AUX_openbsd_timekeep containing timehands information
that is frequently updated by the kernel.

Timing differences between the last kernel update and the current time
are adjusted in userland by the tc_get_timecount() function inside the
MD usertc.c file.

This permits a much more responsive environment, quite visible in
browsers, office programs and gaming (apparently one is are able to fly
in Minecraft now).

Tested by robert@, sthen@, naddy@, kmos@, phessler@, and many others!

OK from at least kettenis@, cheloha@, naddy@, sthen@


Revision tags: OPENBSD_6_7_BASE
# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.58 02-May-2020 kettenis

Fix buglet in previous commit; use time from the struct timeval that was
passed in when setting the RTC time instead of the global time_second.

ok mpi@


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.57 29-Apr-2020 kettenis

Use the same inittodr()/resettodr() implementation as on
amd64/arm64/armv7/sparc64 and move it to the end of machdep.c. Rework the
actual implementation for the MC14818 compatible RTC into something that can
be used as a todr_handle just like on amd64.

ok sthen@


Revision tags: OPENBSD_6_6_BASE
# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.56 22-Aug-2019 deraadt

unused variable, after previous commit


# 1.55 21-Aug-2019 cheloha

sysctl(2): add kern.utc_offset: successor to the DST/TIMEZONE options(4)

The DST and TIMEZONE options(4) are incompatible with KARL, so we need
some other way to compensate for an RTC running with a known offset.

Enter kern.utc_offset, an offset in minutes East of UTC. TIMEZONE has
always been minutes West, but this is inconsistent with how everyone
else talks about timezones, hence the flip.

TIMEZONE has the advantage of being compiled into the binary. Our new
sysctl(2) has no such luck, so it needs to be set as early as possible
in boot, from sysctl.conf(5), so we can correct the kernel clock from
the RTC's local time to UTC before daemons like ntpd(8) and cron(8)
start. To encourage this, kern.utc_offset is made immutable after the
securelevel(7) is raised to 1.

Prompted by yasuoka@. Discussed with deraadt@, kettenis@, yasuoka@.
Additional testing by yasuoka@.

ok deraadt@, yasuoka@


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.54 23-May-2019 jasper

zap reference to non-existent function in a comment

ok mpi@


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


# 1.53 30-Jul-2018 kettenis

Use the MI interrupt enable/distable API instead of the MD one on i386 and
remove the MD API.

ok deraadt@


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision


Revision tags: OPENBSD_6_2_BASE
# 1.52 08-Sep-2017 deraadt

If you use sys/param.h, you don't need sys/types.h


Revision tags: OPENBSD_6_1_BASE
# 1.51 25-Jan-2017 tom

tedu some code that has not been executed since time_t became 64 bits

guenther@ deraadt@


Revision tags: OPENBSD_5_6_BASE OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE OPENBSD_6_0_BASE
# 1.50 29-Mar-2014 guenther

It's been a quarter century: we can assume volatile is present with that name.

ok dlg@ mpi@ deraadt@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.49 06-May-2013 dlg

the use of modern intel performance counter msrs to measure the number of
cycles per second isnt reliable, particularly inside "virtual" machines.
cpuspeed can be calculated as 0, which causes a divide by zero later on
which is bad.

this goes to more effort to detect if the performance counters are in use
by the hypervisor, or detecting if they gave us a cpuspeed of 0 so we can
fall through to using rdtsc.

ok jsg@


# 1.48 17-Apr-2013 gerhard

Don't set the frequency of the statclock if we don't have one.
Prevents strange hang-ups during reboot. Joint work with hshoexer@.

ok mikeb@, mlarkin@, miod@, deraadt@


Revision tags: OPENBSD_5_3_BASE
# 1.47 10-Nov-2012 mglocker

Recent x86 CPUs come with a constant time stamp counter. If this is
the case we verify if the CPU supports a specific version of the
architectural performance monitoring feature and read out the current
frequency from the fixed-function performance counter of the unhalted
core.

My initial motivation to implement this was the Soekris net6501-70
which comes with an Intel Atom E6xx 1.60GHz CPU. It has a constant
time stamp counter plus speed step support and boots on the lowest
frequency of 600MHz. This caused hw.cpuspeed and hw.setperf to
reflect the wrong values.

The diff is a cooperation work with jsg@. The fixed-function
performance counter read code comes from a former diff of him.

OK jsg@


Revision tags: OPENBSD_5_0_BASE OPENBSD_5_1_BASE OPENBSD_5_2_BASE
# 1.46 05-Jul-2011 oga

N: Thou shalt not call hardclock() with biglock held.

i386 disobeys the Nth commandment. Fix this. While here, make i386 and amd64
definitions of iplclock and statclock match.

ok art@, kettenis@


Revision tags: OPENBSD_4_9_BASE
# 1.45 11-Aug-2010 kettenis

Disable the RTC the periodic interrupt. Leaving it enabled causes the
Dell Inspirion 4150 to wake up immediately even though RTC_EN isn't set
in the PM1 Enable register.

ok deraadt@, mlarkin@


# 1.44 10-Aug-2010 marco

spaces and tabs, no binary change


Revision tags: OPENBSD_4_8_BASE
# 1.43 25-Jul-2010 deraadt

in the clock drivers, seperate the soft-state and hard-state which was
all jumbled up in the same functions. the rtc (mc chip) and clock (i8243)
startup was also mixed up. they the soft state and hardware state can
be started in the right order, and it is easy to restart just the
neccessary parts upon resume. tested in numerous cases:
(apic, pic) * (GENERIC.MP, GENERIC) * (mp, non-mp) * (i386, amd64)
ok kettenis


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE OPENBSD_4_7_BASE
# 1.42 29-Jan-2009 kettenis

Only start using the rtc for statclock after we've received the first
interrupt. On some machines the rtc doesn't generate interrupts and we would
end up not running statclock() at all.

ok miod@, art@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.41 28-Nov-2007 tedu

quite a bit of simplification by removing cpu classes.
also assume that 386 cpus are really unknown, and promote them to 486
instead of panic.


Revision tags: OPENBSD_4_2_BASE
# 1.40 01-Aug-2007 martin

switch i386 to use the MI i8253 header file and remove the now obsolete MD
timerreg.h

ok miod@


# 1.39 19-Mar-2007 art

Move i386 to timecounters. This is more or less the same code as amd64.

The "lapic" timer is ripped out since it wasn't actually a lapic timer,
but a hacked up tsc timer with some synchronization for MP. There is no
tsc timer right now since they are very unreliable on MP systems, systems
with apm, and systems that change the cpu clock. Which basically means
every modern machine out there. We're running with the i8259 timer now.

deraadt@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.38 20-Dec-2006 gwk

"#ifdef is a tool of the weak!"
Rename pentium_mhz to cpuspeed which is consistant with amd64 making
shared ACPI code less nasty.
ok marco, deraadt


# 1.37 19-Sep-2006 jsg

ansi/deregister


Revision tags: OPENBSD_4_0_BASE
# 1.36 19-Jun-2006 deraadt

move clock_subr.c to a better place, and now it is always in the kernel
so that things can use it; tested on all architectures; ok kettenis


# 1.35 09-May-2006 otto

Avoid estimating pentium_mhz too low, which has the effect of resulting
in non-monotonic time. Now the gettimeofday regression no longer
fails on my 500MHz PIII. ok toby@ mickey@


Revision tags: OPENBSD_3_9_BASE
# 1.34 12-Feb-2006 miod

Remove dead sysbeep driver and unused sysbeep() routine. No functional change.


# 1.33 02-Jan-2006 brad

remove last traces of __BROKEN_INDIRECT_CONFIG.

Thanks to aanriot@ and Michael Knudsen for checking that alpha/i386/macppc
kernels still compile.


Revision tags: OPENBSD_3_6_BASE OPENBSD_3_7_BASE OPENBSD_3_8_BASE
# 1.32 13-Jun-2004 niklas

debranch SMP, have fun


Revision tags: OPENBSD_3_5_BASE SMP_SYNC_A SMP_SYNC_B
# 1.31 27-Feb-2004 grange

Cleanup I[3456]86_CPU defines usage, unbreaks compilation
without some of them.
Problem reported by William Culler <william@neo.rr.com>.

Help from tedu@ chris@, ok tedu@ chris@ deraadt@.


Revision tags: OPENBSD_3_4_BASE
# 1.30 02-Jun-2003 millert

Remove the advertising clause in the UCB license which Berkeley
rescinded 22 July 1999. Proofed by myself and Theo.


# 1.29 27-May-2003 fgsch

change .byte for the correct opcodes now that gas can handle'em.
art@ toby@ ok.


Revision tags: OPENBSD_3_2_BASE OPENBSD_3_3_BASE UBC_SYNC_A UBC_SYNC_B
# 1.28 06-Jul-2002 nordin

Remove kernel support for NTP. ok deraadt@ and tholo@


# 1.27 17-May-2002 mickey

properly detect the cpu model for the broken cyrix latch; better fix than pr#2661


# 1.26 16-May-2002 mickey

fix for a timer latch bug on the cyrix mediagx and gxm cpus.
based on freebsd pr#6630, netbsd pr#8654, openbsd pr#1492 .
does not affect other cpu models (cyrix or not).
asked by markus@ and testing.


Revision tags: OPENBSD_3_1_BASE
# 1.25 14-Mar-2002 millert

First round of __P removal in sys


Revision tags: OPENBSD_3_0_BASE UBC_BASE
# 1.24 21-Sep-2001 mickey

branches: 1.24.4;
totally useless inlines, cost 1380 bytes too


Revision tags: OPENBSD_2_9_BASE
# 1.23 19-Feb-2001 ho

Avoid losing rtc after suspend/resume on some laptops. Ok provos@


# 1.22 13-Feb-2001 ho

rtc stops if we miss interrupts. Inspired by code from FreeBSD.
Some KNF. (niklas@ ok)


Revision tags: OPENBSD_2_7_BASE OPENBSD_2_8_BASE SMP_BASE
# 1.21 29-Jan-2000 mickey

branches: 1.21.2;
separate clock init, to be called upon resume from apm sleeps


Revision tags: OPENBSD_2_6_BASE kame_19991208
# 1.20 06-Oct-1999 deraadt

y2k related fixes; from netbsd, work by fgsch/ivan


Revision tags: OPENBSD_2_5_BASE
# 1.19 31-Jan-1999 espie

Fix clobbers so that GENERIC may compile with egcs.

Historically, the documentation of extended asm was lacking, namely you
should NOT specify the same register as an input, and a clobber.
If the register is clobbered, it should be specified as an output as well,
e.g., by linking input and output through the "number" notation.

(Beware of lvalues, some local variables needed...)

In older versions, up-to egcs1.1.1, the compiler did not even warn about
it, but it was liable to output bad code. Newer egcs are pickier and
simply refuse to swallow such code.


# 1.18 13-Jan-1999 niklas

RCSIds


# 1.17 02-Jan-1999 niklas

Midi & sequencer support from NetBSD, mostly by Lennart Augustsson


Revision tags: OPENBSD_2_3_BASE OPENBSD_2_4_BASE
# 1.16 17-Dec-1997 downsj

I586_CPU -> I586_CPU || I686_CPU


Revision tags: OPENBSD_2_2_BASE
# 1.15 25-Jun-1997 mickey

remove _STANDALONE
be carefull about leap years (;


Revision tags: OPENBSD_2_1_BASE
# 1.14 11-Apr-1997 maja

If someone pressed a nonprintable character in getsn (eg in boot -c) the
speaker would be turned on but not off. So don't allow sysbeep until
timers works. -moj


# 1.13 16-Oct-1996 deraadt

proto rtcintr


Revision tags: OPENBSD_2_0_BASE
# 1.12 17-Sep-1996 mickey

support standalone.


# 1.11 09-Sep-1996 tholo

Don't be uncertain about wether clock interrupts were meant for us


# 1.10 09-Sep-1996 tholo

Add seperate profiling/statistics clock running at 1024/128 Hz, using the
RTC in periodic interrupt mode


# 1.9 25-May-1996 deraadt

sync


# 1.8 07-May-1996 deraadt

sync with 0504; prototype changes


# 1.7 21-Apr-1996 deraadt

partial sync with netbsd 960418, more to come


# 1.6 20-Mar-1996 mickey

Fix back wrong patches.


# 1.5 19-Mar-1996 mickey

Merging w/ NetBSD 021796.
speaker upgraded to the current.
some changes to the VM stuff (ie kern_thread.c added and so).


# 1.4 25-Feb-1996 tholo

Allow use of Pentium cycle counter for high-precision time keeping;
from FreeBSD


# 1.3 20-Feb-1996 tholo

Pass device name to interrupt establish routines so it can be recorded in
the device interrupt chain structures (isa, pci)

Move interrupt chain structure definition to <machine/psl.h> so vmstat can
get at it (i386)

Remove hack to count interrupts the old way (i386)


# 1.2 27-Dec-1995 deraadt

from netbsd:
The IST_* and IPL_* constants are not bus-specific; don't treat them as such.


# 1.1 18-Oct-1995 deraadt

branches: 1.1.1;
Initial revision