vm_machdep.c revision 189282
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 189282 2009-03-02 18:43:50Z kib $");
45
46#include "opt_isa.h"
47#include "opt_cpu.h"
48#include "opt_compat.h"
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/bio.h>
53#include <sys/buf.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/sysent.h>
66#include <sys/unistd.h>
67#include <sys/vnode.h>
68#include <sys/vmmeter.h>
69
70#include <machine/cpu.h>
71#include <machine/md_var.h>
72#include <machine/pcb.h>
73#include <machine/specialreg.h>
74
75#include <vm/vm.h>
76#include <vm/vm_extern.h>
77#include <vm/vm_kern.h>
78#include <vm/vm_page.h>
79#include <vm/vm_map.h>
80#include <vm/vm_param.h>
81
82#include <amd64/isa/isa.h>
83
84static void	cpu_reset_real(void);
85#ifdef SMP
86static void	cpu_reset_proxy(void);
87static u_int	cpu_reset_proxyid;
88static volatile u_int	cpu_reset_proxy_active;
89#endif
90
91/*
92 * Finish a fork operation, with process p2 nearly set up.
93 * Copy and update the pcb, set up the stack so that the child
94 * ready to run and return to user mode.
95 */
96void
97cpu_fork(td1, p2, td2, flags)
98	register struct thread *td1;
99	register struct proc *p2;
100	struct thread *td2;
101	int flags;
102{
103	register struct proc *p1;
104	struct pcb *pcb2;
105	struct mdproc *mdp2;
106
107	p1 = td1->td_proc;
108	if ((flags & RFPROC) == 0)
109		return;
110
111	/* Ensure that p1's pcb is up to date. */
112	fpuexit(td1);
113
114	/* Point the pcb to the top of the stack */
115	pcb2 = (struct pcb *)(td2->td_kstack +
116	    td2->td_kstack_pages * PAGE_SIZE) - 1;
117	td2->td_pcb = pcb2;
118
119	/* Copy p1's pcb */
120	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
121
122	/* Point mdproc and then copy over td1's contents */
123	mdp2 = &p2->p_md;
124	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
125
126	/*
127	 * Create a new fresh stack for the new process.
128	 * Copy the trap frame for the return to user mode as if from a
129	 * syscall.  This copies most of the user mode register values.
130	 */
131	td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
132	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
133
134	td2->td_frame->tf_rax = 0;		/* Child returns zero */
135	td2->td_frame->tf_rflags &= ~PSL_C;	/* success */
136	td2->td_frame->tf_rdx = 1;
137
138	/*
139	 * If the parent process has the trap bit set (i.e. a debugger had
140	 * single stepped the process to the system call), we need to clear
141	 * the trap flag from the new frame unless the debugger had set PF_FORK
142	 * on the parent.  Otherwise, the child will receive a (likely
143	 * unexpected) SIGTRAP when it executes the first instruction after
144	 * returning  to userland.
145	 */
146	if ((p1->p_pfsflags & PF_FORK) == 0)
147		td2->td_frame->tf_rflags &= ~PSL_T;
148
149	/*
150	 * Set registers for trampoline to user mode.  Leave space for the
151	 * return address on stack.  These are the kernel mode register values.
152	 */
153	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4);
154	pcb2->pcb_r12 = (register_t)fork_return;	/* fork_trampoline argument */
155	pcb2->pcb_rbp = 0;
156	pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
157	pcb2->pcb_rbx = (register_t)td2;		/* fork_trampoline argument */
158	pcb2->pcb_rip = (register_t)fork_trampoline;
159	/*-
160	 * pcb2->pcb_dr*:	cloned above.
161	 * pcb2->pcb_savefpu:	cloned above.
162	 * pcb2->pcb_flags:	cloned above.
163	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
164	 * pcb2->pcb_[fg]sbase:	cloned above
165	 */
166
167	/* Setup to release spin count in fork_exit(). */
168	td2->td_md.md_spinlock_count = 1;
169	td2->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
170
171	/*
172	 * Now, cpu_switch() can schedule the new process.
173	 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
174	 * containing the return address when exiting cpu_switch.
175	 * This will normally be to fork_trampoline(), which will have
176	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
177	 * will set up a stack to call fork_return(p, frame); to complete
178	 * the return to user-mode.
179	 */
180}
181
182/*
183 * Intercept the return address from a freshly forked process that has NOT
184 * been scheduled yet.
185 *
186 * This is needed to make kernel threads stay in kernel mode.
187 */
188void
189cpu_set_fork_handler(td, func, arg)
190	struct thread *td;
191	void (*func)(void *);
192	void *arg;
193{
194	/*
195	 * Note that the trap frame follows the args, so the function
196	 * is really called like this:  func(arg, frame);
197	 */
198	td->td_pcb->pcb_r12 = (long) func;	/* function */
199	td->td_pcb->pcb_rbx = (long) arg;	/* first arg */
200}
201
202void
203cpu_exit(struct thread *td)
204{
205}
206
207void
208cpu_thread_exit(struct thread *td)
209{
210
211	if (td == PCPU_GET(fpcurthread))
212		fpudrop();
213
214	/* Disable any hardware breakpoints. */
215	if (td->td_pcb->pcb_flags & PCB_DBREGS) {
216		reset_dbregs();
217		td->td_pcb->pcb_flags &= ~PCB_DBREGS;
218	}
219}
220
221void
222cpu_thread_clean(struct thread *td)
223{
224}
225
226void
227cpu_thread_swapin(struct thread *td)
228{
229}
230
231void
232cpu_thread_swapout(struct thread *td)
233{
234}
235
236void
237cpu_thread_alloc(struct thread *td)
238{
239
240	td->td_pcb = (struct pcb *)(td->td_kstack +
241	    td->td_kstack_pages * PAGE_SIZE) - 1;
242	td->td_frame = (struct trapframe *)td->td_pcb - 1;
243}
244
245void
246cpu_thread_free(struct thread *td)
247{
248}
249
250/*
251 * Initialize machine state (pcb and trap frame) for a new thread about to
252 * upcall. Put enough state in the new thread's PCB to get it to go back
253 * userret(), where we can intercept it again to set the return (upcall)
254 * Address and stack, along with those from upcals that are from other sources
255 * such as those generated in thread_userret() itself.
256 */
257void
258cpu_set_upcall(struct thread *td, struct thread *td0)
259{
260	struct pcb *pcb2;
261
262	/* Point the pcb to the top of the stack. */
263	pcb2 = td->td_pcb;
264
265	/*
266	 * Copy the upcall pcb.  This loads kernel regs.
267	 * Those not loaded individually below get their default
268	 * values here.
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	 */
276	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
277
278	/* If the current thread has the trap bit set (i.e. a debugger had
279	 * single stepped the process to the system call), we need to clear
280	 * the trap flag from the new frame. Otherwise, the new thread will
281	 * receive a (likely unexpected) SIGTRAP when it executes the first
282	 * instruction after returning to userland.
283	 */
284	td->td_frame->tf_rflags &= ~PSL_T;
285
286	/*
287	 * Set registers for trampoline to user mode.  Leave space for the
288	 * return address on stack.  These are the kernel mode register values.
289	 */
290	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4);
291	pcb2->pcb_r12 = (register_t)fork_return;	    /* trampoline arg */
292	pcb2->pcb_rbp = 0;
293	pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);	/* trampoline arg */
294	pcb2->pcb_rbx = (register_t)td;			    /* trampoline arg */
295	pcb2->pcb_rip = (register_t)fork_trampoline;
296	/*
297	 * If we didn't copy the pcb, we'd need to do the following registers:
298	 * pcb2->pcb_dr*:	cloned above.
299	 * pcb2->pcb_savefpu:	cloned above.
300	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
301	 * pcb2->pcb_[fg]sbase: cloned above
302	 */
303
304	/* Setup to release spin count in fork_exit(). */
305	td->td_md.md_spinlock_count = 1;
306	td->td_md.md_saved_flags = PSL_KERNEL | PSL_I;
307}
308
309/*
310 * Set that machine state for performing an upcall that has to
311 * be done in thread_userret() so that those upcalls generated
312 * in thread_userret() itself can be done as well.
313 */
314void
315cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
316	stack_t *stack)
317{
318
319	/*
320	 * Do any extra cleaning that needs to be done.
321	 * The thread may have optional components
322	 * that are not present in a fresh thread.
323	 * This may be a recycled thread so make it look
324	 * as though it's newly allocated.
325	 */
326	cpu_thread_clean(td);
327
328#ifdef COMPAT_IA32
329	if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
330		/*
331	 	 * Set the trap frame to point at the beginning of the uts
332		 * function.
333		 */
334		td->td_frame->tf_rbp = 0;
335		td->td_frame->tf_rsp =
336		   (((uintptr_t)stack->ss_sp + stack->ss_size - 4) & ~0x0f) - 4;
337		td->td_frame->tf_rip = (uintptr_t)entry;
338
339		/*
340		 * Pass the address of the mailbox for this kse to the uts
341		 * function as a parameter on the stack.
342		 */
343		suword32((void *)(td->td_frame->tf_rsp + sizeof(int32_t)),
344		    (uint32_t)(uintptr_t)arg);
345
346		return;
347	}
348#endif
349
350	/*
351	 * Set the trap frame to point at the beginning of the uts
352	 * function.
353	 */
354	td->td_frame->tf_rbp = 0;
355	td->td_frame->tf_rsp =
356	    ((register_t)stack->ss_sp + stack->ss_size) & ~0x0f;
357	td->td_frame->tf_rsp -= 8;
358	td->td_frame->tf_rip = (register_t)entry;
359
360	/*
361	 * Pass the address of the mailbox for this kse to the uts
362	 * function as a parameter on the stack.
363	 */
364	td->td_frame->tf_rdi = (register_t)arg;
365}
366
367int
368cpu_set_user_tls(struct thread *td, void *tls_base)
369{
370
371	if ((u_int64_t)tls_base >= VM_MAXUSER_ADDRESS)
372		return (EINVAL);
373
374#ifdef COMPAT_IA32
375	if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
376		if (td == curthread) {
377			critical_enter();
378			td->td_pcb->pcb_gsbase = (register_t)tls_base;
379			wrmsr(MSR_KGSBASE, td->td_pcb->pcb_gsbase);
380			critical_exit();
381		} else {
382			td->td_pcb->pcb_gsbase = (register_t)tls_base;
383		}
384		return (0);
385	}
386#endif
387	if (td == curthread) {
388		critical_enter();
389		td->td_pcb->pcb_fsbase = (register_t)tls_base;
390		wrmsr(MSR_FSBASE, td->td_pcb->pcb_fsbase);
391		critical_exit();
392	} else {
393		td->td_pcb->pcb_fsbase = (register_t)tls_base;
394	}
395	return (0);
396}
397
398#ifdef SMP
399static void
400cpu_reset_proxy()
401{
402
403	cpu_reset_proxy_active = 1;
404	while (cpu_reset_proxy_active == 1)
405		;	/* Wait for other cpu to see that we've started */
406	stop_cpus((1<<cpu_reset_proxyid));
407	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
408	DELAY(1000000);
409	cpu_reset_real();
410}
411#endif
412
413void
414cpu_reset()
415{
416#ifdef SMP
417	u_int cnt, map;
418
419	if (smp_active) {
420		map = PCPU_GET(other_cpus) & ~stopped_cpus;
421		if (map != 0) {
422			printf("cpu_reset: Stopping other CPUs\n");
423			stop_cpus(map);
424		}
425
426		if (PCPU_GET(cpuid) != 0) {
427			cpu_reset_proxyid = PCPU_GET(cpuid);
428			cpustop_restartfunc = cpu_reset_proxy;
429			cpu_reset_proxy_active = 0;
430			printf("cpu_reset: Restarting BSP\n");
431
432			/* Restart CPU #0. */
433			atomic_store_rel_int(&started_cpus, 1 << 0);
434
435			cnt = 0;
436			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
437				cnt++;	/* Wait for BSP to announce restart */
438			if (cpu_reset_proxy_active == 0)
439				printf("cpu_reset: Failed to restart BSP\n");
440			enable_intr();
441			cpu_reset_proxy_active = 2;
442
443			while (1);
444			/* NOTREACHED */
445		}
446
447		DELAY(1000000);
448	}
449#endif
450	cpu_reset_real();
451	/* NOTREACHED */
452}
453
454static void
455cpu_reset_real()
456{
457	struct region_descriptor null_idt;
458	int b;
459
460	disable_intr();
461
462	/*
463	 * Attempt to do a CPU reset via the keyboard controller,
464	 * do not turn off GateA20, as any machine that fails
465	 * to do the reset here would then end up in no man's land.
466	 */
467	outb(IO_KBD + 4, 0xFE);
468	DELAY(500000);	/* wait 0.5 sec to see if that did it */
469
470	/*
471	 * Attempt to force a reset via the Reset Control register at
472	 * I/O port 0xcf9.  Bit 2 forces a system reset when it
473	 * transitions from 0 to 1.  Bit 1 selects the type of reset
474	 * to attempt: 0 selects a "soft" reset, and 1 selects a
475	 * "hard" reset.  We try a "hard" reset.  The first write sets
476	 * bit 1 to select a "hard" reset and clears bit 2.  The
477	 * second write forces a 0 -> 1 transition in bit 2 to trigger
478	 * a reset.
479	 */
480	outb(0xcf9, 0x2);
481	outb(0xcf9, 0x6);
482	DELAY(500000);  /* wait 0.5 sec to see if that did it */
483
484	/*
485	 * Attempt to force a reset via the Fast A20 and Init register
486	 * at I/O port 0x92.  Bit 1 serves as an alternate A20 gate.
487	 * Bit 0 asserts INIT# when set to 1.  We are careful to only
488	 * preserve bit 1 while setting bit 0.  We also must clear bit
489	 * 0 before setting it if it isn't already clear.
490	 */
491	b = inb(0x92);
492	if (b != 0xff) {
493		if ((b & 0x1) != 0)
494			outb(0x92, b & 0xfe);
495		outb(0x92, b | 0x1);
496		DELAY(500000);  /* wait 0.5 sec to see if that did it */
497	}
498
499	printf("No known reset method worked, attempting CPU shutdown\n");
500	DELAY(1000000);	/* wait 1 sec for printf to complete */
501
502	/* Wipe the IDT. */
503	null_idt.rd_limit = 0;
504	null_idt.rd_base = 0;
505	lidt(&null_idt);
506
507	/* "good night, sweet prince .... <THUNK!>" */
508	breakpoint();
509
510	/* NOTREACHED */
511	while(1);
512}
513
514/*
515 * Allocate an sf_buf for the given vm_page.  On this machine, however, there
516 * is no sf_buf object.  Instead, an opaque pointer to the given vm_page is
517 * returned.
518 */
519struct sf_buf *
520sf_buf_alloc(struct vm_page *m, int pri)
521{
522
523	return ((struct sf_buf *)m);
524}
525
526/*
527 * Free the sf_buf.  In fact, do nothing because there are no resources
528 * associated with the sf_buf.
529 */
530void
531sf_buf_free(struct sf_buf *sf)
532{
533}
534
535/*
536 * Software interrupt handler for queued VM system processing.
537 */
538void
539swi_vm(void *dummy)
540{
541	if (busdma_swi_pending != 0)
542		busdma_swi();
543}
544
545/*
546 * Tell whether this address is in some physical memory region.
547 * Currently used by the kernel coredump code in order to avoid
548 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
549 * or other unpredictable behaviour.
550 */
551
552int
553is_physical_memory(vm_paddr_t addr)
554{
555
556#ifdef DEV_ISA
557	/* The ISA ``memory hole''. */
558	if (addr >= 0xa0000 && addr < 0x100000)
559		return 0;
560#endif
561
562	/*
563	 * stuff other tests for known memory-mapped devices (PCI?)
564	 * here
565	 */
566
567	return 1;
568}
569