vm_machdep.c revision 119004
1/*-
2 * Copyright (c) 1982, 1986 The Regents of the University of California.
3 * Copyright (c) 1989, 1990 William Jolitz
4 * Copyright (c) 1994 John Dyson
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the Systems Programming Group of the University of Utah Computer
9 * Science Department, and William Jolitz.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *	@(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40 *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/sys/amd64/amd64/vm_machdep.c 119004 2003-08-16 23:15:15Z marcel $");
45
46#include "opt_isa.h"
47#include "opt_kstack_pages.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/malloc.h>
52#include <sys/proc.h>
53#include <sys/kse.h>
54#include <sys/bio.h>
55#include <sys/buf.h>
56#include <sys/vnode.h>
57#include <sys/vmmeter.h>
58#include <sys/kernel.h>
59#include <sys/ktr.h>
60#include <sys/mutex.h>
61#include <sys/sysctl.h>
62#include <sys/unistd.h>
63
64#include <machine/cpu.h>
65#include <machine/md_var.h>
66#include <machine/pcb.h>
67
68#include <vm/vm.h>
69#include <vm/vm_param.h>
70#include <sys/lock.h>
71#include <vm/vm_kern.h>
72#include <vm/vm_page.h>
73#include <vm/vm_map.h>
74#include <vm/vm_extern.h>
75
76#include <sys/user.h>
77
78#include <amd64/isa/isa.h>
79
80static void	cpu_reset_real(void);
81
82/*
83 * Finish a fork operation, with process p2 nearly set up.
84 * Copy and update the pcb, set up the stack so that the child
85 * ready to run and return to user mode.
86 */
87void
88cpu_fork(td1, p2, td2, flags)
89	register struct thread *td1;
90	register struct proc *p2;
91	struct thread *td2;
92	int flags;
93{
94	register struct proc *p1;
95	struct pcb *pcb2;
96	struct mdproc *mdp2;
97	register_t savecrit;
98
99	p1 = td1->td_proc;
100	if ((flags & RFPROC) == 0)
101		return;
102
103	/* Ensure that p1's pcb is up to date. */
104	savecrit = intr_disable();
105	if (PCPU_GET(fpcurthread) == td1)
106		npxsave(&td1->td_pcb->pcb_save);
107	intr_restore(savecrit);
108
109	/* Point the pcb to the top of the stack */
110	pcb2 = (struct pcb *)(td2->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
111	td2->td_pcb = pcb2;
112
113	/* Copy p1's pcb */
114	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
115
116	/* Point mdproc and then copy over td1's contents */
117	mdp2 = &p2->p_md;
118	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
119
120	/*
121	 * Create a new fresh stack for the new process.
122	 * Copy the trap frame for the return to user mode as if from a
123	 * syscall.  This copies most of the user mode register values.
124	 */
125	td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
126	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
127
128	td2->td_frame->tf_rax = 0;		/* Child returns zero */
129	td2->td_frame->tf_rflags &= ~PSL_C;	/* success */
130	td2->td_frame->tf_rdx = 1;
131
132	/*
133	 * Set registers for trampoline to user mode.  Leave space for the
134	 * return address on stack.  These are the kernel mode register values.
135	 */
136	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4);
137	pcb2->pcb_r12 = (register_t)fork_return;	/* fork_trampoline argument */
138	pcb2->pcb_rbp = 0;
139	pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
140	pcb2->pcb_rbx = (register_t)td2;		/* fork_trampoline argument */
141	pcb2->pcb_rip = (register_t)fork_trampoline;
142	pcb2->pcb_rflags = td2->td_frame->tf_rflags & ~PSL_I; /* ints disabled */
143	/*-
144	 * pcb2->pcb_savefpu:	cloned above.
145	 * pcb2->pcb_flags:	cloned above.
146	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
147	 * pcb2->pcb_[fg]sbase:	cloned above
148	 */
149
150	/*
151	 * Now, cpu_switch() can schedule the new process.
152	 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
153	 * containing the return address when exiting cpu_switch.
154	 * This will normally be to fork_trampoline(), which will have
155	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
156	 * will set up a stack to call fork_return(p, frame); to complete
157	 * the return to user-mode.
158	 */
159}
160
161/*
162 * Intercept the return address from a freshly forked process that has NOT
163 * been scheduled yet.
164 *
165 * This is needed to make kernel threads stay in kernel mode.
166 */
167void
168cpu_set_fork_handler(td, func, arg)
169	struct thread *td;
170	void (*func)(void *);
171	void *arg;
172{
173	/*
174	 * Note that the trap frame follows the args, so the function
175	 * is really called like this:  func(arg, frame);
176	 */
177	td->td_pcb->pcb_r12 = (long) func;	/* function */
178	td->td_pcb->pcb_rbx = (long) arg;	/* first arg */
179}
180
181void
182cpu_exit(struct thread *td)
183{
184	struct mdproc *mdp;
185
186	mdp = &td->td_proc->p_md;
187}
188
189void
190cpu_thread_exit(struct thread *td)
191{
192
193	npxexit(td);
194}
195
196void
197cpu_thread_clean(struct thread *td)
198{
199}
200
201void
202cpu_thread_swapin(struct thread *td)
203{
204}
205
206void
207cpu_thread_swapout(struct thread *td)
208{
209}
210
211void
212cpu_sched_exit(td)
213	register struct thread *td;
214{
215}
216
217void
218cpu_thread_setup(struct thread *td)
219{
220
221	td->td_pcb =
222	     (struct pcb *)(td->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
223	td->td_frame = (struct trapframe *)td->td_pcb - 1;
224}
225
226/*
227 * Initialize machine state (pcb and trap frame) for a new thread about to
228 * upcall. Pu t enough state in the new thread's PCB to get it to go back
229 * userret(), where we can intercept it again to set the return (upcall)
230 * Address and stack, along with those from upcals that are from other sources
231 * such as those generated in thread_userret() itself.
232 */
233void
234cpu_set_upcall(struct thread *td, struct thread *td0)
235{
236	struct pcb *pcb2;
237
238	/* Point the pcb to the top of the stack. */
239	pcb2 = td->td_pcb;
240
241	/*
242	 * Copy the upcall pcb.  This loads kernel regs.
243	 * Those not loaded individually below get their default
244	 * values here.
245	 *
246	 * XXXKSE It might be a good idea to simply skip this as
247	 * the values of the other registers may be unimportant.
248	 * This would remove any requirement for knowing the KSE
249	 * at this time (see the matching comment below for
250	 * more analysis) (need a good safe default).
251	 */
252	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
253
254	/*
255	 * Create a new fresh stack for the new thread.
256	 * Don't forget to set this stack value into whatever supplies
257	 * the address for the fault handlers.
258	 * The contexts are filled in at the time we actually DO the
259	 * upcall as only then do we know which KSE we got.
260	 */
261	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
262
263	/*
264	 * Set registers for trampoline to user mode.  Leave space for the
265	 * return address on stack.  These are the kernel mode register values.
266	 */
267	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4);
268	pcb2->pcb_r12 = (register_t)fork_return;	    /* trampoline arg */
269	pcb2->pcb_rbp = 0;
270	pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);	/* trampoline arg */
271	pcb2->pcb_rbx = (register_t)td;			    /* trampoline arg */
272	pcb2->pcb_rip = (register_t)fork_trampoline;
273	pcb2->pcb_rflags = PSL_KERNEL; /* ints disabled */
274	/*
275	 * If we didn't copy the pcb, we'd need to do the following registers:
276	 * pcb2->pcb_savefpu:	cloned above.
277	 * pcb2->pcb_rflags:	cloned above.
278	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
279	 * pcb2->pcb_[fg]sbase: cloned above
280	 */
281}
282
283/*
284 * Set that machine state for performing an upcall that has to
285 * be done in thread_userret() so that those upcalls generated
286 * in thread_userret() itself can be done as well.
287 */
288void
289cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
290{
291
292	/*
293	 * Do any extra cleaning that needs to be done.
294	 * The thread may have optional components
295	 * that are not present in a fresh thread.
296	 * This may be a recycled thread so make it look
297	 * as though it's newly allocated.
298	 */
299	cpu_thread_clean(td);
300
301	/*
302	 * Set the trap frame to point at the beginning of the uts
303	 * function.
304	 */
305	td->td_frame->tf_rsp =
306	    ((register_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size) & ~0x0f;
307	td->td_frame->tf_rsp -= 8;
308	td->td_frame->tf_rip = (register_t)ku->ku_func;
309
310	/*
311	 * Pass the address of the mailbox for this kse to the uts
312	 * function as a parameter on the stack.
313	 */
314	td->td_frame->tf_rdi = (register_t)ku->ku_mailbox;
315}
316
317
318/*
319 * Force reset the processor by invalidating the entire address space!
320 */
321
322void
323cpu_reset()
324{
325	cpu_reset_real();
326}
327
328static void
329cpu_reset_real()
330{
331
332	/*
333	 * Attempt to do a CPU reset via the keyboard controller,
334	 * do not turn of the GateA20, as any machine that fails
335	 * to do the reset here would then end up in no man's land.
336	 */
337
338	outb(IO_KBD + 4, 0xFE);
339	DELAY(500000);	/* wait 0.5 sec to see if that did it */
340	printf("Keyboard reset did not work, attempting CPU shutdown\n");
341	DELAY(1000000);	/* wait 1 sec for printf to complete */
342	/* force a shutdown by unmapping entire address space ! */
343	bzero((caddr_t)PML4map, PAGE_SIZE);
344
345	/* "good night, sweet prince .... <THUNK!>" */
346	invltlb();
347	/* NOTREACHED */
348	while(1);
349}
350
351/*
352 * Software interrupt handler for queued VM system processing.
353 */
354void
355swi_vm(void *dummy)
356{
357	if (busdma_swi_pending != 0)
358		busdma_swi();
359}
360
361/*
362 * Tell whether this address is in some physical memory region.
363 * Currently used by the kernel coredump code in order to avoid
364 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
365 * or other unpredictable behaviour.
366 */
367
368int
369is_physical_memory(addr)
370	vm_offset_t addr;
371{
372
373#ifdef DEV_ISA
374	/* The ISA ``memory hole''. */
375	if (addr >= 0xa0000 && addr < 0x100000)
376		return 0;
377#endif
378
379	/*
380	 * stuff other tests for known memory-mapped devices (PCI?)
381	 * here
382	 */
383
384	return 1;
385}
386