xref: /freebsd/sys/i386/i386/vm_machdep.c (revision e0c27215058b5786c78fcfb3963eebe61a989511)
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$");
45 
46 #include "opt_npx.h"
47 #ifdef PC98
48 #include "opt_pc98.h"
49 #endif
50 #include "opt_reset.h"
51 #include "opt_isa.h"
52 #include "opt_kstack_pages.h"
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/malloc.h>
57 #include <sys/proc.h>
58 #include <sys/kse.h>
59 #include <sys/bio.h>
60 #include <sys/buf.h>
61 #include <sys/vnode.h>
62 #include <sys/vmmeter.h>
63 #include <sys/kernel.h>
64 #include <sys/ktr.h>
65 #include <sys/mutex.h>
66 #include <sys/smp.h>
67 #include <sys/sysctl.h>
68 #include <sys/unistd.h>
69 
70 #include <machine/cpu.h>
71 #include <machine/md_var.h>
72 #include <machine/pcb.h>
73 #include <machine/pcb_ext.h>
74 #include <machine/vm86.h>
75 
76 #include <vm/vm.h>
77 #include <vm/vm_param.h>
78 #include <sys/lock.h>
79 #include <vm/vm_kern.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_extern.h>
83 
84 #include <sys/user.h>
85 
86 #ifdef PC98
87 #include <pc98/pc98/pc98.h>
88 #else
89 #include <i386/isa/isa.h>
90 #endif
91 
92 static void	cpu_reset_real(void);
93 #ifdef SMP
94 static void	cpu_reset_proxy(void);
95 static u_int	cpu_reset_proxyid;
96 static volatile u_int	cpu_reset_proxy_active;
97 #endif
98 extern int	_ucodesel, _udatasel;
99 
100 /*
101  * Finish a fork operation, with process p2 nearly set up.
102  * Copy and update the pcb, set up the stack so that the child
103  * ready to run and return to user mode.
104  */
105 void
106 cpu_fork(td1, p2, td2, flags)
107 	register struct thread *td1;
108 	register struct proc *p2;
109 	struct thread *td2;
110 	int flags;
111 {
112 	register struct proc *p1;
113 	struct pcb *pcb2;
114 	struct mdproc *mdp2;
115 #ifdef DEV_NPX
116 	register_t savecrit;
117 #endif
118 
119 	p1 = td1->td_proc;
120 	if ((flags & RFPROC) == 0) {
121 		if ((flags & RFMEM) == 0) {
122 			/* unshare user LDT */
123 			struct mdproc *mdp1 = &p1->p_md;
124 			struct proc_ldt *pldt = mdp1->md_ldt;
125 			if (pldt && pldt->ldt_refcnt > 1) {
126 				pldt = user_ldt_alloc(mdp1, pldt->ldt_len);
127 				if (pldt == NULL)
128 					panic("could not copy LDT");
129 				mdp1->md_ldt = pldt;
130 				set_user_ldt(mdp1);
131 				user_ldt_free(td1);
132 			}
133 		}
134 		return;
135 	}
136 
137 	/* Ensure that p1's pcb is up to date. */
138 #ifdef DEV_NPX
139 	if (td1 == curthread)
140 		td1->td_pcb->pcb_gs = rgs();
141 	savecrit = intr_disable();
142 	if (PCPU_GET(fpcurthread) == td1)
143 		npxsave(&td1->td_pcb->pcb_save);
144 	intr_restore(savecrit);
145 #endif
146 
147 	/* Point the pcb to the top of the stack */
148 	pcb2 = (struct pcb *)(td2->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
149 	td2->td_pcb = pcb2;
150 
151 	/* Copy p1's pcb */
152 	bcopy(td1->td_pcb, pcb2, sizeof(*pcb2));
153 
154 	/* Point mdproc and then copy over td1's contents */
155 	mdp2 = &p2->p_md;
156 	bcopy(&p1->p_md, mdp2, sizeof(*mdp2));
157 
158 	/*
159 	 * Create a new fresh stack for the new process.
160 	 * Copy the trap frame for the return to user mode as if from a
161 	 * syscall.  This copies most of the user mode register values.
162 	 * The -16 is so we can expand the trapframe if we go to vm86.
163 	 */
164 	td2->td_frame = (struct trapframe *)((caddr_t)td2->td_pcb - 16) - 1;
165 	bcopy(td1->td_frame, td2->td_frame, sizeof(struct trapframe));
166 
167 	td2->td_frame->tf_eax = 0;		/* Child returns zero */
168 	td2->td_frame->tf_eflags &= ~PSL_C;	/* success */
169 	td2->td_frame->tf_edx = 1;
170 
171 	/*
172 	 * Set registers for trampoline to user mode.  Leave space for the
173 	 * return address on stack.  These are the kernel mode register values.
174 	 */
175 #ifdef PAE
176 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdpt);
177 #else
178 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
179 #endif
180 	pcb2->pcb_edi = 0;
181 	pcb2->pcb_esi = (int)fork_return;	/* fork_trampoline argument */
182 	pcb2->pcb_ebp = 0;
183 	pcb2->pcb_esp = (int)td2->td_frame - sizeof(void *);
184 	pcb2->pcb_ebx = (int)td2;		/* fork_trampoline argument */
185 	pcb2->pcb_eip = (int)fork_trampoline;
186 	pcb2->pcb_psl = PSL_KERNEL;		/* ints disabled */
187 	pcb2->pcb_gs = rgs();
188 	/*-
189 	 * pcb2->pcb_dr*:	cloned above.
190 	 * pcb2->pcb_savefpu:	cloned above.
191 	 * pcb2->pcb_flags:	cloned above.
192 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
193 	 * pcb2->pcb_gs:	cloned above.
194 	 * pcb2->pcb_ext:	cleared below.
195 	 */
196 
197 	/*
198 	 * XXX don't copy the i/o pages.  this should probably be fixed.
199 	 */
200 	pcb2->pcb_ext = 0;
201 
202         /* Copy the LDT, if necessary. */
203 	mtx_lock_spin(&sched_lock);
204         if (mdp2->md_ldt != 0) {
205 		if (flags & RFMEM) {
206 			mdp2->md_ldt->ldt_refcnt++;
207 		} else {
208 			mdp2->md_ldt = user_ldt_alloc(mdp2,
209 			    mdp2->md_ldt->ldt_len);
210 			if (mdp2->md_ldt == NULL)
211 				panic("could not copy LDT");
212 		}
213         }
214 	mtx_unlock_spin(&sched_lock);
215 
216 	/*
217 	 * Now, cpu_switch() can schedule the new process.
218 	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
219 	 * containing the return address when exiting cpu_switch.
220 	 * This will normally be to fork_trampoline(), which will have
221 	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
222 	 * will set up a stack to call fork_return(p, frame); to complete
223 	 * the return to user-mode.
224 	 */
225 }
226 
227 /*
228  * Intercept the return address from a freshly forked process that has NOT
229  * been scheduled yet.
230  *
231  * This is needed to make kernel threads stay in kernel mode.
232  */
233 void
234 cpu_set_fork_handler(td, func, arg)
235 	struct thread *td;
236 	void (*func)(void *);
237 	void *arg;
238 {
239 	/*
240 	 * Note that the trap frame follows the args, so the function
241 	 * is really called like this:  func(arg, frame);
242 	 */
243 	td->td_pcb->pcb_esi = (int) func;	/* function */
244 	td->td_pcb->pcb_ebx = (int) arg;	/* first arg */
245 }
246 
247 void
248 cpu_exit(struct thread *td)
249 {
250 	struct mdproc *mdp;
251 	struct pcb *pcb = td->td_pcb;
252 
253 
254 	/* Reset pc->pcb_gs and %gs before possibly invalidating it. */
255 	mdp = &td->td_proc->p_md;
256 	if (mdp->md_ldt) {
257 		td->td_pcb->pcb_gs = _udatasel;
258 		load_gs(_udatasel);
259 		user_ldt_free(td);
260 	}
261 	if (pcb->pcb_flags & PCB_DBREGS) {
262 		/* disable all hardware breakpoints */
263 		reset_dbregs();
264 		pcb->pcb_flags &= ~PCB_DBREGS;
265 	}
266 }
267 
268 void
269 cpu_thread_exit(struct thread *td)
270 {
271 	struct pcb *pcb = td->td_pcb;
272 #ifdef DEV_NPX
273 	npxexit(td);
274 #endif
275         if (pcb->pcb_flags & PCB_DBREGS) {
276 		/* disable all hardware breakpoints */
277                 reset_dbregs();
278                 pcb->pcb_flags &= ~PCB_DBREGS;
279         }
280 }
281 
282 void
283 cpu_thread_clean(struct thread *td)
284 {
285 	struct pcb *pcb;
286 
287 	pcb = td->td_pcb;
288 	if (pcb->pcb_ext != 0) {
289 		/* XXXKSE  XXXSMP  not SMP SAFE.. what locks do we have? */
290 		/* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */
291 		/*
292 		 * XXX do we need to move the TSS off the allocated pages
293 		 * before freeing them?  (not done here)
294 		 */
295 		mtx_lock(&Giant);
296 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
297 		    ctob(IOPAGES + 1));
298 		mtx_unlock(&Giant);
299 		pcb->pcb_ext = 0;
300 	}
301 }
302 
303 void
304 cpu_sched_exit(td)
305 	register struct thread *td;
306 {
307 }
308 
309 void
310 cpu_thread_setup(struct thread *td)
311 {
312 
313 	td->td_pcb =
314 	     (struct pcb *)(td->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
315 	td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb - 16) - 1;
316 	td->td_pcb->pcb_ext = NULL;
317 }
318 
319 /*
320  * Initialize machine state (pcb and trap frame) for a new thread about to
321  * upcall. Pu t enough state in the new thread's PCB to get it to go back
322  * userret(), where we can intercept it again to set the return (upcall)
323  * Address and stack, along with those from upcals that are from other sources
324  * such as those generated in thread_userret() itself.
325  */
326 void
327 cpu_set_upcall(struct thread *td, struct thread *td0)
328 {
329 	struct pcb *pcb2;
330 
331 	/* Point the pcb to the top of the stack. */
332 	pcb2 = td->td_pcb;
333 
334 	/*
335 	 * Copy the upcall pcb.  This loads kernel regs.
336 	 * Those not loaded individually below get their default
337 	 * values here.
338 	 *
339 	 * XXXKSE It might be a good idea to simply skip this as
340 	 * the values of the other registers may be unimportant.
341 	 * This would remove any requirement for knowing the KSE
342 	 * at this time (see the matching comment below for
343 	 * more analysis) (need a good safe default).
344 	 */
345 	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
346 
347 	/*
348 	 * Create a new fresh stack for the new thread.
349 	 * The -16 is so we can expand the trapframe if we go to vm86.
350 	 * Don't forget to set this stack value into whatever supplies
351 	 * the address for the fault handlers.
352 	 * The contexts are filled in at the time we actually DO the
353 	 * upcall as only then do we know which KSE we got.
354 	 */
355 	bcopy(td0->td_frame, td->td_frame, sizeof(struct trapframe));
356 
357 	/*
358 	 * Set registers for trampoline to user mode.  Leave space for the
359 	 * return address on stack.  These are the kernel mode register values.
360 	 */
361 #ifdef PAE
362 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdpt);
363 #else
364 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdir);
365 #endif
366 	pcb2->pcb_edi = 0;
367 	pcb2->pcb_esi = (int)fork_return;		    /* trampoline arg */
368 	pcb2->pcb_ebp = 0;
369 	pcb2->pcb_esp = (int)td->td_frame - sizeof(void *); /* trampoline arg */
370 	pcb2->pcb_ebx = (int)td;			    /* trampoline arg */
371 	pcb2->pcb_eip = (int)fork_trampoline;
372 	pcb2->pcb_psl &= ~(PSL_I);	/* interrupts must be disabled */
373 	pcb2->pcb_gs = rgs();
374 	/*
375 	 * If we didn't copy the pcb, we'd need to do the following registers:
376 	 * pcb2->pcb_dr*:	cloned above.
377 	 * pcb2->pcb_savefpu:	cloned above.
378 	 * pcb2->pcb_flags:	cloned above.
379 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
380 	 * pcb2->pcb_gs:	cloned above.  XXXKSE ???
381 	 * pcb2->pcb_ext:	cleared below.
382 	 */
383 	 pcb2->pcb_ext = NULL;
384 }
385 
386 /*
387  * Set that machine state for performing an upcall that has to
388  * be done in thread_userret() so that those upcalls generated
389  * in thread_userret() itself can be done as well.
390  */
391 void
392 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
393 {
394 
395 	/*
396 	 * Do any extra cleaning that needs to be done.
397 	 * The thread may have optional components
398 	 * that are not present in a fresh thread.
399 	 * This may be a recycled thread so make it look
400 	 * as though it's newly allocated.
401 	 */
402 	cpu_thread_clean(td);
403 
404 	/*
405 	 * Set the trap frame to point at the beginning of the uts
406 	 * function.
407 	 */
408 	td->td_frame->tf_esp =
409 	    (int)ku->ku_stack.ss_sp + ku->ku_stack.ss_size - 16;
410 	td->td_frame->tf_eip = (int)ku->ku_func;
411 
412 	/*
413 	 * Pass the address of the mailbox for this kse to the uts
414 	 * function as a parameter on the stack.
415 	 */
416 	suword((void *)(td->td_frame->tf_esp + sizeof(void *)),
417 	    (int)ku->ku_mailbox);
418 }
419 
420 /*
421  * Convert kernel VA to physical address
422  */
423 vm_paddr_t
424 kvtop(void *addr)
425 {
426 	vm_paddr_t pa;
427 
428 	pa = pmap_kextract((vm_offset_t)addr);
429 	if (pa == 0)
430 		panic("kvtop: zero page frame");
431 	return (pa);
432 }
433 
434 /*
435  * Force reset the processor by invalidating the entire address space!
436  */
437 
438 #ifdef SMP
439 static void
440 cpu_reset_proxy()
441 {
442 
443 	cpu_reset_proxy_active = 1;
444 	while (cpu_reset_proxy_active == 1)
445 		;	 /* Wait for other cpu to see that we've started */
446 	stop_cpus((1<<cpu_reset_proxyid));
447 	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
448 	DELAY(1000000);
449 	cpu_reset_real();
450 }
451 #endif
452 
453 void
454 cpu_reset()
455 {
456 #ifdef SMP
457 	if (smp_active == 0) {
458 		cpu_reset_real();
459 		/* NOTREACHED */
460 	} else {
461 
462 		u_int map;
463 		int cnt;
464 		printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid));
465 
466 		map = PCPU_GET(other_cpus) & ~ stopped_cpus;
467 
468 		if (map != 0) {
469 			printf("cpu_reset: Stopping other CPUs\n");
470 			stop_cpus(map);		/* Stop all other CPUs */
471 		}
472 
473 		if (PCPU_GET(cpuid) == 0) {
474 			DELAY(1000000);
475 			cpu_reset_real();
476 			/* NOTREACHED */
477 		} else {
478 			/* We are not BSP (CPU #0) */
479 
480 			cpu_reset_proxyid = PCPU_GET(cpuid);
481 			cpustop_restartfunc = cpu_reset_proxy;
482 			cpu_reset_proxy_active = 0;
483 			printf("cpu_reset: Restarting BSP\n");
484 			started_cpus = (1<<0);		/* Restart CPU #0 */
485 
486 			cnt = 0;
487 			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
488 				cnt++;	/* Wait for BSP to announce restart */
489 			if (cpu_reset_proxy_active == 0)
490 				printf("cpu_reset: Failed to restart BSP\n");
491 			enable_intr();
492 			cpu_reset_proxy_active = 2;
493 
494 			while (1);
495 			/* NOTREACHED */
496 		}
497 	}
498 #else
499 	cpu_reset_real();
500 #endif
501 }
502 
503 static void
504 cpu_reset_real()
505 {
506 
507 #ifdef PC98
508 	/*
509 	 * Attempt to do a CPU reset via CPU reset port.
510 	 */
511 	disable_intr();
512 	if ((inb(0x35) & 0xa0) != 0xa0) {
513 		outb(0x37, 0x0f);		/* SHUT0 = 0. */
514 		outb(0x37, 0x0b);		/* SHUT1 = 0. */
515 	}
516 	outb(0xf0, 0x00);		/* Reset. */
517 #else
518 	/*
519 	 * Attempt to do a CPU reset via the keyboard controller,
520 	 * do not turn of the GateA20, as any machine that fails
521 	 * to do the reset here would then end up in no man's land.
522 	 */
523 
524 #if !defined(BROKEN_KEYBOARD_RESET)
525 	outb(IO_KBD + 4, 0xFE);
526 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
527 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
528 	DELAY(1000000);	/* wait 1 sec for printf to complete */
529 #endif
530 #endif /* PC98 */
531 	/* force a shutdown by unmapping entire address space ! */
532 	bzero((caddr_t)PTD, NBPTD);
533 
534 	/* "good night, sweet prince .... <THUNK!>" */
535 	invltlb();
536 	/* NOTREACHED */
537 	while(1);
538 }
539 
540 /*
541  * Software interrupt handler for queued VM system processing.
542  */
543 void
544 swi_vm(void *dummy)
545 {
546 	if (busdma_swi_pending != 0)
547 		busdma_swi();
548 }
549 
550 /*
551  * Tell whether this address is in some physical memory region.
552  * Currently used by the kernel coredump code in order to avoid
553  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
554  * or other unpredictable behaviour.
555  */
556 
557 int
558 is_physical_memory(addr)
559 	vm_offset_t addr;
560 {
561 
562 #ifdef DEV_ISA
563 	/* The ISA ``memory hole''. */
564 	if (addr >= 0xa0000 && addr < 0x100000)
565 		return 0;
566 #endif
567 
568 	/*
569 	 * stuff other tests for known memory-mapped devices (PCI?)
570 	 * here
571 	 */
572 
573 	return 1;
574 }
575