]>
git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/hardclock.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990 Carnegie Mellon University
31 * All Rights Reserved.
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.
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.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
60 #include <time_stamp.h>
62 #include <kern/cpu_number.h>
63 #include <kern/cpu_data.h>
64 #include <kern/kern_types.h>
65 #include <platforms.h>
67 #include <mach_kprof.h>
68 #include <mach_mp_debug.h>
69 #include <mach/std_types.h>
71 #include <mach/clock_types.h>
72 #include <mach/boolean.h>
73 #include <i386/thread.h>
74 #include <i386/eflags.h>
75 #include <kern/assert.h>
76 #include <kern/misc_protos.h>
77 #include <i386/misc_protos.h>
78 #include <kern/time_out.h>
82 #include <i386/hardclock_entries.h>
83 #include <i386/rtclock_entries.h>
86 #include <i386/mach_param.h> /* for HZ */
87 #endif /* MACH_MP_DEBUG */
89 extern char return_to_iret
[];
91 #if TIME_STAMP && NCPUS > 1
92 extern unsigned time_stamp
;
93 unsigned old_time_stamp
, time_stamp_cum
, nstamps
;
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
100 #define time_stamp_stat() \
102 if (!old_time_stamp) { \
103 old_time_stamp = time_stamp; \
107 time_stamp_cum = (time_stamp - old_time_stamp); \
109 #else /* TIME_STAMP && AT386 && NCPUS > 1 */
110 #define time_stamp_stat()
111 #endif /* TIME_STAMP && AT386 && NCPUS > 1 */
114 int masked_pc
[NCPUS
];
115 int missed_clock
[NCPUS
];
116 int detect_lost_tick
= 0;
117 #endif /* MACH_KPROF */
120 int masked_state_cnt
[NCPUS
];
121 int masked_state_max
= 10*HZ
;
122 #endif /* MACH_MP_DEBUG */
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).
138 int paranoid_debugger
= TRUE
;
139 int paranoid_count
= 1000;
140 int paranoid_current
= 0;
141 int paranoid_cpu
= 0;
142 #endif /* PARANOID_KDB */
145 hardclock(struct i386_interrupt_state
*regs
) /* saved registers */
148 register unsigned pc
;
149 register boolean_t usermode
;
151 mp_disable_preemption();
152 mycpu
= cpu_number();
155 if (paranoid_cpu
== mycpu
&&
156 paranoid_current
++ >= paranoid_count
) {
157 paranoid_current
= 0;
158 if (paranoid_debugger
)
159 Debugger("hardclock");
161 #endif /* PARANOID_KDB */
166 * Increments counter of clock ticks handled under a masked state.
167 * Debugger() is called if masked state is kept during 1 sec.
168 * The counter is reset by splx() when ipl mask is set back to SPL0,
171 if (SPL_CMP_GT((old_ipl
& 0xFF), SPL0
)) {
172 if (masked_state_cnt
[mycpu
]++ >= masked_state_max
) {
173 int max_save
= masked_state_max
;
175 masked_state_cnt
[mycpu
] = 0;
176 masked_state_max
= 0x7fffffff;
178 if (ret_addr
== return_to_iret
) {
179 usermode
= (regs
->efl
& EFL_VM
) ||
180 ((regs
->cs
& 0x03) != 0);
181 pc
= (unsigned)regs
->eip
;
185 ((struct i386_interrupt_state
*)&old_ipl
)->eip
;
187 printf("looping at high IPL, usermode=%d pc=0x%x\n",
191 masked_state_cnt
[mycpu
] = 0;
192 masked_state_max
= max_save
;
195 masked_state_cnt
[mycpu
] = 0;
196 #endif /* MACH_MP_DEBUG */
201 * If we were masked against the clock skip call
202 * to rtclock_intr(). When MACH_KPROF is set, the
203 * clock frequency of the master-cpu is confined
206 if (SPL_CMP_LT(old_ipl
& 0xFF, SPL7
))
207 #endif /* MACH_KPROF */
209 * The master processor executes the rtclock_intr() routine
210 * on every clock tick. The rtclock_intr() routine returns
211 * a zero value on a HZ tick boundary.
213 if (mycpu
== master_cpu
) {
214 if (rtclock_intr() != 0) {
215 mp_enable_preemption();
221 * The following code is executed at HZ rate by all processors
222 * in the system. This implies that the clock rate on slave
223 * processors must be HZ rate.
229 if (ret_addr
== return_to_iret
) {
231 * A kernel-loaded task executing within itself will look like
232 * "kernel mode", here. This is correct with syscalls
233 * implemented using migrating threads, because it means that
234 * the time spent in the server by a client thread will be
235 * treated as "system" time for the client thread (and nothing
236 * for the server). This conforms to the CPU reporting for an
240 usermode
= (regs
->efl
& EFL_VM
) || ((regs
->cs
& 0x03) != 0);
241 pc
= (unsigned)regs
->eip
;
245 pc
= (unsigned)((struct i386_interrupt_state
*)&old_ipl
)->eip
;
251 * If we were masked against the clock, just memorize pc
252 * and the fact that the clock interrupt is delayed
254 if (SPL_CMP_GE((old_ipl
& 0xFF), SPL7
)) {
256 if (missed_clock
[mycpu
]++ && detect_lost_tick
> 1)
257 Debugger("Mach_KPROF");
258 masked_pc
[mycpu
] = pc
;
260 #endif /* MACH_KPROF */
262 hertz_tick(usermode
, pc
);
266 * Instead of having the master processor interrupt
267 * all active processors, each processor in turn interrupts
268 * the next active one. This avoids all slave processors
269 * accessing the same R/W data simultaneously.
272 #endif /* NCPUS >1 && AT386 */
274 mp_enable_preemption();
284 mp_disable_preemption();
285 my_cpu
= cpu_number();
287 if (missed_clock
[my_cpu
] > 1 && detect_lost_tick
)
288 printf("hardclock: missed %d clock interrupt(s) at %x\n",
289 missed_clock
[my_cpu
]-1, masked_pc
[my_cpu
]);
290 if (my_cpu
== master_cpu
) {
294 hertz_tick(0, masked_pc
[my_cpu
]);
295 missed_clock
[my_cpu
] = 0;
297 mp_enable_preemption();
299 #endif /* MACH_KPROF */