vm_machdep.c revision 123884
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 123884 2003-12-27 07:52:47Z silby $");
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/mbuf.h>
61#include <sys/mutex.h>
62#include <sys/sf_buf.h>
63#include <sys/smp.h>
64#include <sys/sysctl.h>
65#include <sys/unistd.h>
66
67#include <machine/cpu.h>
68#include <machine/md_var.h>
69#include <machine/pcb.h>
70
71#include <vm/vm.h>
72#include <vm/vm_param.h>
73#include <sys/lock.h>
74#include <vm/vm_kern.h>
75#include <vm/vm_page.h>
76#include <vm/vm_map.h>
77#include <vm/vm_extern.h>
78
79#include <sys/user.h>
80
81#include <amd64/isa/isa.h>
82
83static void	cpu_reset_real(void);
84#ifdef SMP
85static void	cpu_reset_proxy(void);
86static u_int	cpu_reset_proxyid;
87static volatile u_int	cpu_reset_proxy_active;
88#endif
89static void	sf_buf_init(void *arg);
90SYSINIT(sock_sf, SI_SUB_MBUF, SI_ORDER_ANY, sf_buf_init, NULL)
91
92/*
93 * Expanded sf_freelist head. Really an SLIST_HEAD() in disguise, with the
94 * sf_freelist head with the sf_lock mutex.
95 */
96static struct {
97	SLIST_HEAD(, sf_buf) sf_head;
98	struct mtx sf_lock;
99} sf_freelist;
100
101static u_int	sf_buf_alloc_want;
102extern int	nsfbufspeak, nsfbufsused;
103
104/*
105 * Finish a fork operation, with process p2 nearly set up.
106 * Copy and update the pcb, set up the stack so that the child
107 * ready to run and return to user mode.
108 */
109void
110cpu_fork(td1, p2, td2, flags)
111	register struct thread *td1;
112	register struct proc *p2;
113	struct thread *td2;
114	int flags;
115{
116	register struct proc *p1;
117	struct pcb *pcb2;
118	struct mdproc *mdp2;
119	register_t savecrit;
120
121	p1 = td1->td_proc;
122	if ((flags & RFPROC) == 0)
123		return;
124
125	/* Ensure that p1's pcb is up to date. */
126	savecrit = intr_disable();
127	if (PCPU_GET(fpcurthread) == td1)
128		fpusave(&td1->td_pcb->pcb_save);
129	intr_restore(savecrit);
130
131	/* Point the pcb to the top of the stack */
132	pcb2 = (struct pcb *)(td2->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
133	td2->td_pcb = pcb2;
134
135	/* Copy p1's pcb */
136	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
137
138	/* Point mdproc and then copy over td1's contents */
139	mdp2 = &p2->p_md;
140	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
141
142	/*
143	 * Create a new fresh stack for the new process.
144	 * Copy the trap frame for the return to user mode as if from a
145	 * syscall.  This copies most of the user mode register values.
146	 */
147	td2->td_frame = (struct trapframe *)td2->td_pcb - 1;
148	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
149
150	td2->td_frame->tf_rax = 0;		/* Child returns zero */
151	td2->td_frame->tf_rflags &= ~PSL_C;	/* success */
152	td2->td_frame->tf_rdx = 1;
153
154	/*
155	 * Set registers for trampoline to user mode.  Leave space for the
156	 * return address on stack.  These are the kernel mode register values.
157	 */
158	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pml4);
159	pcb2->pcb_r12 = (register_t)fork_return;	/* fork_trampoline argument */
160	pcb2->pcb_rbp = 0;
161	pcb2->pcb_rsp = (register_t)td2->td_frame - sizeof(void *);
162	pcb2->pcb_rbx = (register_t)td2;		/* fork_trampoline argument */
163	pcb2->pcb_rip = (register_t)fork_trampoline;
164	pcb2->pcb_rflags = td2->td_frame->tf_rflags & ~PSL_I; /* ints disabled */
165	/*-
166	 * pcb2->pcb_savefpu:	cloned above.
167	 * pcb2->pcb_flags:	cloned above.
168	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
169	 * pcb2->pcb_[fg]sbase:	cloned above
170	 */
171
172	/*
173	 * Now, cpu_switch() can schedule the new process.
174	 * pcb_rsp is loaded pointing to the cpu_switch() stack frame
175	 * containing the return address when exiting cpu_switch.
176	 * This will normally be to fork_trampoline(), which will have
177	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
178	 * will set up a stack to call fork_return(p, frame); to complete
179	 * the return to user-mode.
180	 */
181}
182
183/*
184 * Intercept the return address from a freshly forked process that has NOT
185 * been scheduled yet.
186 *
187 * This is needed to make kernel threads stay in kernel mode.
188 */
189void
190cpu_set_fork_handler(td, func, arg)
191	struct thread *td;
192	void (*func)(void *);
193	void *arg;
194{
195	/*
196	 * Note that the trap frame follows the args, so the function
197	 * is really called like this:  func(arg, frame);
198	 */
199	td->td_pcb->pcb_r12 = (long) func;	/* function */
200	td->td_pcb->pcb_rbx = (long) arg;	/* first arg */
201}
202
203void
204cpu_exit(struct thread *td)
205{
206	struct mdproc *mdp;
207
208	mdp = &td->td_proc->p_md;
209}
210
211void
212cpu_thread_exit(struct thread *td)
213{
214
215	if (td == PCPU_GET(fpcurthread))
216		fpudrop();
217}
218
219void
220cpu_thread_clean(struct thread *td)
221{
222}
223
224void
225cpu_thread_swapin(struct thread *td)
226{
227}
228
229void
230cpu_thread_swapout(struct thread *td)
231{
232}
233
234void
235cpu_sched_exit(td)
236	register struct thread *td;
237{
238}
239
240void
241cpu_thread_setup(struct thread *td)
242{
243
244	td->td_pcb =
245	     (struct pcb *)(td->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
246	td->td_frame = (struct trapframe *)td->td_pcb - 1;
247}
248
249/*
250 * Initialize machine state (pcb and trap frame) for a new thread about to
251 * upcall. Pu t enough state in the new thread's PCB to get it to go back
252 * userret(), where we can intercept it again to set the return (upcall)
253 * Address and stack, along with those from upcals that are from other sources
254 * such as those generated in thread_userret() itself.
255 */
256void
257cpu_set_upcall(struct thread *td, struct thread *td0)
258{
259	struct pcb *pcb2;
260
261	/* Point the pcb to the top of the stack. */
262	pcb2 = td->td_pcb;
263
264	/*
265	 * Copy the upcall pcb.  This loads kernel regs.
266	 * Those not loaded individually below get their default
267	 * values here.
268	 *
269	 * XXXKSE It might be a good idea to simply skip this as
270	 * the values of the other registers may be unimportant.
271	 * This would remove any requirement for knowing the KSE
272	 * at this time (see the matching comment below for
273	 * more analysis) (need a good safe default).
274	 */
275	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
276	pcb2->pcb_flags &= ~PCB_FPUINITDONE;
277
278	/*
279	 * Create a new fresh stack for the new thread.
280	 * Don't forget to set this stack value into whatever supplies
281	 * the address for the fault handlers.
282	 * The contexts are filled in at the time we actually DO the
283	 * upcall as only then do we know which KSE we got.
284	 */
285	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
286
287	/*
288	 * Set registers for trampoline to user mode.  Leave space for the
289	 * return address on stack.  These are the kernel mode register values.
290	 */
291	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pml4);
292	pcb2->pcb_r12 = (register_t)fork_return;	    /* trampoline arg */
293	pcb2->pcb_rbp = 0;
294	pcb2->pcb_rsp = (register_t)td->td_frame - sizeof(void *);	/* trampoline arg */
295	pcb2->pcb_rbx = (register_t)td;			    /* trampoline arg */
296	pcb2->pcb_rip = (register_t)fork_trampoline;
297	pcb2->pcb_rflags = PSL_KERNEL; /* ints disabled */
298	/*
299	 * If we didn't copy the pcb, we'd need to do the following registers:
300	 * pcb2->pcb_savefpu:	cloned above.
301	 * pcb2->pcb_rflags:	cloned above.
302	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
303	 * pcb2->pcb_[fg]sbase: cloned above
304	 */
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_rsp =
330	    ((register_t)ku->ku_stack.ss_sp + ku->ku_stack.ss_size) & ~0x0f;
331	td->td_frame->tf_rsp -= 8;
332	td->td_frame->tf_rip = (register_t)ku->ku_func;
333
334	/*
335	 * Pass the address of the mailbox for this kse to the uts
336	 * function as a parameter on the stack.
337	 */
338	td->td_frame->tf_rdi = (register_t)ku->ku_mailbox;
339}
340
341
342/*
343 * Force reset the processor by invalidating the entire address space!
344 */
345
346#ifdef SMP
347static void
348cpu_reset_proxy()
349{
350
351	cpu_reset_proxy_active = 1;
352	while (cpu_reset_proxy_active == 1)
353		;	 /* Wait for other cpu to see that we've started */
354	stop_cpus((1<<cpu_reset_proxyid));
355	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
356	DELAY(1000000);
357	cpu_reset_real();
358}
359#endif
360
361void
362cpu_reset()
363{
364#ifdef SMP
365	if (smp_active == 0) {
366		cpu_reset_real();
367		/* NOTREACHED */
368	} else {
369
370		u_int map;
371		int cnt;
372		printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid));
373
374		map = PCPU_GET(other_cpus) & ~ stopped_cpus;
375
376		if (map != 0) {
377			printf("cpu_reset: Stopping other CPUs\n");
378			stop_cpus(map);		/* Stop all other CPUs */
379		}
380
381		if (PCPU_GET(cpuid) == 0) {
382			DELAY(1000000);
383			cpu_reset_real();
384			/* NOTREACHED */
385		} else {
386			/* We are not BSP (CPU #0) */
387
388			cpu_reset_proxyid = PCPU_GET(cpuid);
389			cpustop_restartfunc = cpu_reset_proxy;
390			cpu_reset_proxy_active = 0;
391			printf("cpu_reset: Restarting BSP\n");
392			started_cpus = (1<<0);		/* Restart CPU #0 */
393
394			cnt = 0;
395			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
396				cnt++;	/* Wait for BSP to announce restart */
397			if (cpu_reset_proxy_active == 0)
398				printf("cpu_reset: Failed to restart BSP\n");
399			enable_intr();
400			cpu_reset_proxy_active = 2;
401
402			while (1);
403			/* NOTREACHED */
404		}
405	}
406#else
407	cpu_reset_real();
408#endif
409}
410
411static void
412cpu_reset_real()
413{
414
415	/*
416	 * Attempt to do a CPU reset via the keyboard controller,
417	 * do not turn of the GateA20, as any machine that fails
418	 * to do the reset here would then end up in no man's land.
419	 */
420
421	outb(IO_KBD + 4, 0xFE);
422	DELAY(500000);	/* wait 0.5 sec to see if that did it */
423	printf("Keyboard reset did not work, attempting CPU shutdown\n");
424	DELAY(1000000);	/* wait 1 sec for printf to complete */
425	/* force a shutdown by unmapping entire address space ! */
426	bzero((caddr_t)PML4map, PAGE_SIZE);
427
428	/* "good night, sweet prince .... <THUNK!>" */
429	invltlb();
430	/* NOTREACHED */
431	while(1);
432}
433
434/*
435 * Allocate a pool of sf_bufs (sendfile(2) or "super-fast" if you prefer. :-))
436 */
437static void
438sf_buf_init(void *arg)
439{
440	struct sf_buf *sf_bufs;
441	int i;
442
443	mtx_init(&sf_freelist.sf_lock, "sf_bufs list lock", NULL, MTX_DEF);
444	SLIST_INIT(&sf_freelist.sf_head);
445	sf_bufs = malloc(nsfbufs * sizeof(struct sf_buf), M_TEMP,
446	    M_NOWAIT | M_ZERO);
447	for (i = 0; i < nsfbufs; i++)
448		SLIST_INSERT_HEAD(&sf_freelist.sf_head, &sf_bufs[i], free_list);
449	sf_buf_alloc_want = 0;
450}
451
452/*
453 * Get an sf_buf from the freelist. Will block if none are available.
454 */
455struct sf_buf *
456sf_buf_alloc(struct vm_page *m)
457{
458	struct sf_buf *sf;
459	int error;
460
461	mtx_lock(&sf_freelist.sf_lock);
462	while ((sf = SLIST_FIRST(&sf_freelist.sf_head)) == NULL) {
463		sf_buf_alloc_want++;
464		error = msleep(&sf_freelist, &sf_freelist.sf_lock, PVM|PCATCH,
465		    "sfbufa", 0);
466		sf_buf_alloc_want--;
467
468		/*
469		 * If we got a signal, don't risk going back to sleep.
470		 */
471		if (error)
472			break;
473	}
474	if (sf != NULL) {
475		SLIST_REMOVE_HEAD(&sf_freelist.sf_head, free_list);
476		sf->m = m;
477		nsfbufsused++;
478		nsfbufspeak = max(nsfbufspeak, nsfbufsused);
479	}
480	mtx_unlock(&sf_freelist.sf_lock);
481	return (sf);
482}
483
484/*
485 * Detatch mapped page and release resources back to the system.
486 */
487void
488sf_buf_free(void *addr, void *args)
489{
490	struct sf_buf *sf;
491	struct vm_page *m;
492
493	sf = args;
494	m = sf->m;
495	vm_page_lock_queues();
496	vm_page_unwire(m, 0);
497	/*
498	 * Check for the object going away on us. This can
499	 * happen since we don't hold a reference to it.
500	 * If so, we're responsible for freeing the page.
501	 */
502	if (m->wire_count == 0 && m->object == NULL)
503		vm_page_free(m);
504	vm_page_unlock_queues();
505	sf->m = NULL;
506	mtx_lock(&sf_freelist.sf_lock);
507	SLIST_INSERT_HEAD(&sf_freelist.sf_head, sf, free_list);
508	nsfbufsused--;
509	if (sf_buf_alloc_want > 0)
510		wakeup_one(&sf_freelist);
511	mtx_unlock(&sf_freelist.sf_lock);
512}
513
514/*
515 * Software interrupt handler for queued VM system processing.
516 */
517void
518swi_vm(void *dummy)
519{
520	if (busdma_swi_pending != 0)
521		busdma_swi();
522}
523
524/*
525 * Tell whether this address is in some physical memory region.
526 * Currently used by the kernel coredump code in order to avoid
527 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
528 * or other unpredictable behaviour.
529 */
530
531int
532is_physical_memory(addr)
533	vm_offset_t addr;
534{
535
536#ifdef DEV_ISA
537	/* The ISA ``memory hole''. */
538	if (addr >= 0xa0000 && addr < 0x100000)
539		return 0;
540#endif
541
542	/*
543	 * stuff other tests for known memory-mapped devices (PCI?)
544	 * here
545	 */
546
547	return 1;
548}
549