vm_machdep.c revision 144637
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 *	from: @(#)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 144637 2005-04-04 21:53:56Z jhb $");
45
46#include "opt_isa.h"
47#include "opt_cpu.h"
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/bio.h>
52#include <sys/buf.h>
53#include <sys/kse.h>
54#include <sys/kernel.h>
55#include <sys/ktr.h>
56#include <sys/lock.h>
57#include <sys/malloc.h>
58#include <sys/mbuf.h>
59#include <sys/mutex.h>
60#include <sys/pioctl.h>
61#include <sys/proc.h>
62#include <sys/sf_buf.h>
63#include <sys/smp.h>
64#include <sys/sysctl.h>
65#include <sys/unistd.h>
66#include <sys/vnode.h>
67#include <sys/vmmeter.h>
68
69#include <machine/cpu.h>
70#include <machine/md_var.h>
71#include <machine/pcb.h>
72
73#include <vm/vm.h>
74#include <vm/vm_extern.h>
75#include <vm/vm_kern.h>
76#include <vm/vm_page.h>
77#include <vm/vm_map.h>
78#include <vm/vm_param.h>
79
80#include <amd64/isa/isa.h>
81
82static void	cpu_reset_real(void);
83#ifdef SMP
84static void	cpu_reset_proxy(void);
85static u_int	cpu_reset_proxyid;
86static volatile u_int	cpu_reset_proxy_active;
87#endif
88
89/*
90 * Finish a fork operation, with process p2 nearly set up.
91 * Copy and update the pcb, set up the stack so that the child
92 * ready to run and return to user mode.
93 */
94void
95cpu_fork(td1, p2, td2, flags)
96	register struct thread *td1;
97	register struct proc *p2;
98	struct thread *td2;
99	int flags;
100{
101	register struct proc *p1;
102	struct pcb *pcb2;
103	struct mdproc *mdp2;
104
105	p1 = td1->td_proc;
106	if ((flags & RFPROC) == 0)
107		return;
108
109	/* Ensure that p1's pcb is up to date. */
110	fpuexit(td1);
111
112	/* Point the pcb to the top of the stack */
113	pcb2 = (struct pcb *)(td2->td_kstack +
114	    td2->td_kstack_pages * PAGE_SIZE) - 1;
115	td2->td_pcb = pcb2;
116
117	/* Copy p1's pcb */
118	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
119
120	/* Point mdproc and then copy over td1's contents */
121	mdp2 = &p2->p_md;
122	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
123
124	/*
125	 * Create a new fresh stack for the new process.
126	 * Copy the trap frame for the return to user mode as if from a
127	 * syscall.  This copies most of the user mode register values.
128	 */
129	td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
130	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
131
132	td2->td_frame->tf_rax = 0;		/* Child returns zero */
133	td2->td_frame->tf_rflags &= ~PSL_C;	/* success */
134	td2->td_frame->tf_rdx = 1;
135
136	/*
137	 * If the parent process has the trap bit set (i.e. a debugger had
138	 * single stepped the process to the system call), we need to clear
139	 * the trap flag from the new frame unless the debugger had set PF_FORK
140	 * on the parent.  Otherwise, the child will receive a (likely
141	 * unexpected) SIGTRAP when it executes the first instruction after
142	 * returning  to userland.
143	 */
144	if ((p1->p_pfsflags & PF_FORK) == 0)
145		td2->td_frame->tf_rflags &= ~PSL_T;
146
147	/*
148	 * Set registers for trampoline to user mode.  Leave space for the
149	 * return address on stack.  These are the kernel mode register values.
150	 */
151	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4);
152	pcb2->pcb_r12 = (register_t)fork_return;	/* fork_trampoline argument */
153	pcb2->pcb_rbp = 0;
154	pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
155	pcb2->pcb_rbx = (register_t)td2;		/* fork_trampoline argument */
156	pcb2->pcb_rip = (register_t)fork_trampoline;
157	pcb2->pcb_rflags = td2->td_frame->tf_rflags & ~PSL_I; /* ints disabled */
158	/*-
159	 * pcb2->pcb_dr*:	cloned above.
160	 * pcb2->pcb_savefpu:	cloned above.
161	 * pcb2->pcb_flags:	cloned above.
162	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
163	 * pcb2->pcb_[fg]sbase:	cloned above
164	 */
165
166	/* Setup to release sched_lock in fork_exit(). */
167	td2->td_md.md_spinlock_count = 1;
168	td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
169
170	/*
171	 * Now, cpu_switch() can schedule the new process.
172	 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
173	 * containing the return address when exiting cpu_switch.
174	 * This will normally be to fork_trampoline(), which will have
175	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
176	 * will set up a stack to call fork_return(p, frame); to complete
177	 * the return to user-mode.
178	 */
179}
180
181/*
182 * Intercept the return address from a freshly forked process that has NOT
183 * been scheduled yet.
184 *
185 * This is needed to make kernel threads stay in kernel mode.
186 */
187void
188cpu_set_fork_handler(td, func, arg)
189	struct thread *td;
190	void (*func)(void *);
191	void *arg;
192{
193	/*
194	 * Note that the trap frame follows the args, so the function
195	 * is really called like this:  func(arg, frame);
196	 */
197	td->td_pcb->pcb_r12 = (long) func;	/* function */
198	td->td_pcb->pcb_rbx = (long) arg;	/* first arg */
199}
200
201void
202cpu_exit(struct thread *td)
203{
204}
205
206void
207cpu_thread_exit(struct thread *td)
208{
209	struct pcb *pcb = td->td_pcb;
210
211	if (td == PCPU_GET(fpcurthread))
212		fpudrop();
213	if (pcb->pcb_flags & PCB_DBREGS) {
214		/* disable all hardware breakpoints */
215		reset_dbregs();
216		pcb->pcb_flags &= ~PCB_DBREGS;
217	}
218}
219
220void
221cpu_thread_clean(struct thread *td)
222{
223}
224
225void
226cpu_thread_swapin(struct thread *td)
227{
228}
229
230void
231cpu_thread_swapout(struct thread *td)
232{
233}
234
235void
236cpu_thread_setup(struct thread *td)
237{
238
239	td->td_pcb = (struct pcb *)(td->td_kstack +
240	    td->td_kstack_pages * PAGE_SIZE) - 1;
241	td->td_frame = (struct trapframe *)td->td_pcb - 1;
242}
243
244/*
245 * Initialize machine state (pcb and trap frame) for a new thread about to
246 * upcall. Put enough state in the new thread's PCB to get it to go back
247 * userret(), where we can intercept it again to set the return (upcall)
248 * Address and stack, along with those from upcals that are from other sources
249 * such as those generated in thread_userret() itself.
250 */
251void
252cpu_set_upcall(struct thread *td, struct thread *td0)
253{
254	struct pcb *pcb2;
255
256	/* Point the pcb to the top of the stack. */
257	pcb2 = td->td_pcb;
258
259	/*
260	 * Copy the upcall pcb.  This loads kernel regs.
261	 * Those not loaded individually below get their default
262	 * values here.
263	 *
264	 * XXXKSE It might be a good idea to simply skip this as
265	 * the values of the other registers may be unimportant.
266	 * This would remove any requirement for knowing the KSE
267	 * at this time (see the matching comment below for
268	 * more analysis) (need a good safe default).
269	 */
270	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
271	pcb2->pcb_flags &= ~PCB_FPUINITDONE;
272
273	/*
274	 * Create a new fresh stack for the new thread.
275	 * Don't forget to set this stack value into whatever supplies
276	 * the address for the fault handlers.
277	 * The contexts are filled in at the time we actually DO the
278	 * upcall as only then do we know which KSE we got.
279	 */
280	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
281
282	/*
283	 * Set registers for trampoline to user mode.  Leave space for the
284	 * return address on stack.  These are the kernel mode register values.
285	 */
286	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4);
287	pcb2->pcb_r12 = (register_t)fork_return;	    /* trampoline arg */
288	pcb2->pcb_rbp = 0;
289	pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);	/* trampoline arg */
290	pcb2->pcb_rbx = (register_t)td;			    /* trampoline arg */
291	pcb2->pcb_rip = (register_t)fork_trampoline;
292	pcb2->pcb_rflags = PSL_KERNEL; /* ints disabled */
293	/*
294	 * If we didn't copy the pcb, we'd need to do the following registers:
295	 * pcb2->pcb_dr*:	cloned above.
296	 * pcb2->pcb_savefpu:	cloned above.
297	 * pcb2->pcb_rflags:	cloned above.
298	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
299	 * pcb2->pcb_[fg]sbase: cloned above
300	 */
301
302	/* Setup to release sched_lock in fork_exit(). */
303	td->td_md.md_spinlock_count = 1;
304	td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
305}
306
307/*
308 * Set that machine state for performing an upcall that has to
309 * be done in thread_userret() so that those upcalls generated
310 * in thread_userret() itself can be done as well.
311 */
312void
313cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
314{
315
316	/*
317	 * Do any extra cleaning that needs to be done.
318	 * The thread may have optional components
319	 * that are not present in a fresh thread.
320	 * This may be a recycled thread so make it look
321	 * as though it's newly allocated.
322	 */
323	cpu_thread_clean(td);
324
325	/*
326	 * Set the trap frame to point at the beginning of the uts
327	 * function.
328	 */
329	td->td_frame->tf_rbp = 0;
330	td->td_frame->tf_rsp =
331	    ((register_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size) & ~0x0f;
332	td->td_frame->tf_rsp -= 8;
333	td->td_frame->tf_rbp = 0;
334	td->td_frame->tf_rip = (register_t)ku->ku_func;
335
336	/*
337	 * Pass the address of the mailbox for this kse to the uts
338	 * function as a parameter on the stack.
339	 */
340	td->td_frame->tf_rdi = (register_t)ku->ku_mailbox;
341}
342
343#ifdef SMP
344static void
345cpu_reset_proxy()
346{
347
348	cpu_reset_proxy_active = 1;
349	while (cpu_reset_proxy_active == 1)
350		;	/* Wait for other cpu to see that we've started */
351	stop_cpus((1<<cpu_reset_proxyid));
352	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
353	DELAY(1000000);
354	cpu_reset_real();
355}
356#endif
357
358void
359cpu_reset()
360{
361#ifdef SMP
362	u_int cnt, map;
363
364	if (smp_active) {
365		map = PCPU_GET(other_cpus) & ~stopped_cpus;
366		if (map != 0) {
367			printf("cpu_reset: Stopping other CPUs\n");
368			stop_cpus(map);
369		}
370
371		if (PCPU_GET(cpuid) != 0) {
372			cpu_reset_proxyid = PCPU_GET(cpuid);
373			cpustop_restartfunc = cpu_reset_proxy;
374			cpu_reset_proxy_active = 0;
375			printf("cpu_reset: Restarting BSP\n");
376			started_cpus = (1<<0);		/* Restart CPU #0 */
377
378			cnt = 0;
379			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
380				cnt++;	/* Wait for BSP to announce restart */
381			if (cpu_reset_proxy_active == 0)
382				printf("cpu_reset: Failed to restart BSP\n");
383			enable_intr();
384			cpu_reset_proxy_active = 2;
385
386			while (1);
387			/* NOTREACHED */
388		}
389
390		DELAY(1000000);
391	}
392#endif
393	cpu_reset_real();
394	/* NOTREACHED */
395}
396
397static void
398cpu_reset_real()
399{
400
401	/*
402	 * Attempt to do a CPU reset via the keyboard controller,
403	 * do not turn off GateA20, as any machine that fails
404	 * to do the reset here would then end up in no man's land.
405	 */
406	outb(IO_KBD + 4, 0xFE);
407	DELAY(500000);	/* wait 0.5 sec to see if that did it */
408	printf("Keyboard reset did not work, attempting CPU shutdown\n");
409	DELAY(1000000);	/* wait 1 sec for printf to complete */
410
411	/* Force a shutdown by unmapping entire address space. */
412	bzero((caddr_t)PML4map, PAGE_SIZE);
413
414	/* "good night, sweet prince .... <THUNK!>" */
415	invltlb();
416	/* NOTREACHED */
417	while(1);
418}
419
420/*
421 * Allocate an sf_buf for the given vm_page.  On this machine, however, there
422 * is no sf_buf object.  Instead, an opaque pointer to the given vm_page is
423 * returned.
424 */
425struct sf_buf *
426sf_buf_alloc(struct vm_page *m, int pri)
427{
428
429	return ((struct sf_buf *)m);
430}
431
432/*
433 * Free the sf_buf.  In fact, do nothing because there are no resources
434 * associated with the sf_buf.
435 */
436void
437sf_buf_free(struct sf_buf *sf)
438{
439}
440
441/*
442 * Software interrupt handler for queued VM system processing.
443 */
444void
445swi_vm(void *dummy)
446{
447	if (busdma_swi_pending != 0)
448		busdma_swi();
449}
450
451/*
452 * Tell whether this address is in some physical memory region.
453 * Currently used by the kernel coredump code in order to avoid
454 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
455 * or other unpredictable behaviour.
456 */
457
458int
459is_physical_memory(vm_paddr_t addr)
460{
461
462#ifdef DEV_ISA
463	/* The ISA ``memory hole''. */
464	if (addr >= 0xa0000 && addr < 0x100000)
465		return 0;
466#endif
467
468	/*
469	 * stuff other tests for known memory-mapped devices (PCI?)
470	 * here
471	 */
472
473	return 1;
474}
475