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