]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/hardclock.c
xnu-517.3.15.tar.gz
[apple/xnu.git] / osfmk / i386 / hardclock.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * @OSF_COPYRIGHT@
27 */
28/*
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
32 *
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
38 *
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
42 *
43 * Carnegie Mellon requests users of this software to return to
44 *
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
49 *
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
52 */
53/*
54 */
55
56/*
57 * Clock interrupt.
58 */
59#include <cpus.h>
60#include <time_stamp.h>
61#include <mach_kdb.h>
62#include <kern/cpu_number.h>
63#include <kern/cpu_data.h>
64#include <kern/kern_types.h>
65#include <platforms.h>
1c79356b
A
66#include <mach_kprof.h>
67#include <mach_mp_debug.h>
68#include <mach/std_types.h>
69
70#include <mach/clock_types.h>
71#include <mach/boolean.h>
72#include <i386/thread.h>
73#include <i386/eflags.h>
74#include <kern/assert.h>
75#include <kern/misc_protos.h>
76#include <i386/misc_protos.h>
77#include <kern/time_out.h>
55e303ae 78#include <kern/processor.h>
1c79356b
A
79
80#include <i386/ipl.h>
81
82#include <i386/hardclock_entries.h>
83#include <i386/rtclock_entries.h>
84
85#if MACH_MP_DEBUG
86#include <i386/mach_param.h> /* for HZ */
87#endif /* MACH_MP_DEBUG */
88
89extern char return_to_iret[];
90
91#if TIME_STAMP && NCPUS > 1
92extern unsigned time_stamp;
93unsigned old_time_stamp, time_stamp_cum, nstamps;
94
95/*
96 * If H/W provides a counter, record number of ticks and cumulated
97 * time stamps to know timestamps rate.
98 * This should go away when ALARMCLOCKS installed
99 */
100#define time_stamp_stat() \
101 if (my_cpu == 0) \
102 if (!old_time_stamp) { \
103 old_time_stamp = time_stamp; \
104 nstamps = 0; \
105 } else { \
106 nstamps++; \
107 time_stamp_cum = (time_stamp - old_time_stamp); \
108 }
109#else /* TIME_STAMP && AT386 && NCPUS > 1 */
110#define time_stamp_stat()
111#endif /* TIME_STAMP && AT386 && NCPUS > 1 */
112
113#if MACH_KPROF
114int masked_pc[NCPUS];
115int missed_clock[NCPUS];
116int detect_lost_tick = 0;
117#endif /* MACH_KPROF */
118
119#if MACH_MP_DEBUG
120int masked_state_cnt[NCPUS];
121int masked_state_max = 10*HZ;
122#endif /* MACH_MP_DEBUG */
123
124/*
125 * In the interest of a fast clock interrupt service path,
126 * this routine should be folded into assembly language with
127 * a direct interrupt vector on the i386. The "pit" interrupt
128 * should always call the rtclock_intr() routine on the master
129 * processor. The return value of the rtclock_intr() routine
130 * indicates whether HZ rate clock processing should be
131 * performed. (On the Sequent, all slave processors will
132 * run at HZ rate). For now, we'll leave this routine in C
133 * (with TIME_STAMP, MACH_MP_DEBUG and MACH_KPROF code this
134 * routine is way too large for assembler anyway).
135 */
136
137#ifdef PARANOID_KDB
138int paranoid_debugger = TRUE;
139int paranoid_count = 1000;
140int paranoid_current = 0;
141int paranoid_cpu = 0;
142#endif /* PARANOID_KDB */
143
144void
145hardclock(struct i386_interrupt_state *regs) /* saved registers */
146{
147 int mycpu;
148 register unsigned pc;
149 register boolean_t usermode;
150
151 mp_disable_preemption();
152 mycpu = cpu_number();
153
154#ifdef PARANOID_KDB
155 if (paranoid_cpu == mycpu &&
156 paranoid_current++ >= paranoid_count) {
157 paranoid_current = 0;
158 if (paranoid_debugger)
159 Debugger("hardclock");
160 }
161#endif /* PARANOID_KDB */
162
1c79356b
A
163#if MACH_KPROF
164 /*
165 * If we were masked against the clock skip call
166 * to rtclock_intr(). When MACH_KPROF is set, the
167 * clock frequency of the master-cpu is confined
168 * to the HZ rate.
169 */
55e303ae
A
170 if (SPL_CMP_GE((old_ipl & 0xFF), SPL7)) {
171 usermode = (regs->efl & EFL_VM) || ((regs->cs & 0x03) != 0);
172 pc = (unsigned)regs->eip;
173 assert(!usermode);
174 if (missed_clock[mycpu]++ && detect_lost_tick > 1)
175 Debugger("Mach_KPROF");
176 masked_pc[mycpu] = pc;
177 } else
1c79356b
A
178#endif /* MACH_KPROF */
179 /*
180 * The master processor executes the rtclock_intr() routine
181 * on every clock tick. The rtclock_intr() routine returns
182 * a zero value on a HZ tick boundary.
183 */
184 if (mycpu == master_cpu) {
55e303ae 185 if (rtclock_intr(regs) != 0) {
1c79356b
A
186 mp_enable_preemption();
187 return;
188 }
55e303ae
A
189 } else {
190 usermode = (regs->efl & EFL_VM) || ((regs->cs & 0x03) != 0);
191 pc = (unsigned)regs->eip;
192 hertz_tick(usermode, pc);
1c79356b
A
193 }
194
195 /*
196 * The following code is executed at HZ rate by all processors
197 * in the system. This implies that the clock rate on slave
198 * processors must be HZ rate.
199 */
200
201 time_stamp_stat();
202
1c79356b
A
203#if NCPUS >1
204 /*
205 * Instead of having the master processor interrupt
206 * all active processors, each processor in turn interrupts
207 * the next active one. This avoids all slave processors
208 * accessing the same R/W data simultaneously.
209 */
210 slave_clock();
211#endif /* NCPUS >1 && AT386 */
212
213 mp_enable_preemption();
214}
215
216#if MACH_KPROF
217void
218delayed_clock(void)
219{
220 int i;
221 int my_cpu;
222
223 mp_disable_preemption();
224 my_cpu = cpu_number();
225
226 if (missed_clock[my_cpu] > 1 && detect_lost_tick)
227 printf("hardclock: missed %d clock interrupt(s) at %x\n",
228 missed_clock[my_cpu]-1, masked_pc[my_cpu]);
229 if (my_cpu == master_cpu) {
230 i = rtclock_intr();
231 assert(i == 0);
232 }
233 hertz_tick(0, masked_pc[my_cpu]);
234 missed_clock[my_cpu] = 0;
235
236 mp_enable_preemption();
237}
238#endif /* MACH_KPROF */