xref: /freebsd/sys/i386/i386/vm_machdep.c (revision 390e8cc2974df1888369c06339ef8e0e92b312b6)
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 
252 	/* Reset pc->pcb_gs and %gs before possibly invalidating it. */
253 	mdp = &td->td_proc->p_md;
254 	if (mdp->md_ldt) {
255 		td->td_pcb->pcb_gs = _udatasel;
256 		load_gs(_udatasel);
257 		user_ldt_free(td);
258 	}
259 	reset_dbregs();
260 }
261 
262 void
263 cpu_thread_exit(struct thread *td)
264 {
265 	struct pcb *pcb = td->td_pcb;
266 #ifdef DEV_NPX
267 	npxexit(td);
268 #endif
269         if (pcb->pcb_flags & PCB_DBREGS) {
270                 /*
271                  * disable all hardware breakpoints
272                  */
273                 reset_dbregs();
274                 pcb->pcb_flags &= ~PCB_DBREGS;
275         }
276 }
277 
278 void
279 cpu_thread_clean(struct thread *td)
280 {
281 	struct pcb *pcb;
282 
283 	pcb = td->td_pcb;
284 	if (pcb->pcb_ext != 0) {
285 		/* XXXKSE  XXXSMP  not SMP SAFE.. what locks do we have? */
286 		/* if (pcb->pcb_ext->ext_refcount-- == 1) ?? */
287 		/*
288 		 * XXX do we need to move the TSS off the allocated pages
289 		 * before freeing them?  (not done here)
290 		 */
291 		mtx_lock(&Giant);
292 		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
293 		    ctob(IOPAGES + 1));
294 		mtx_unlock(&Giant);
295 		pcb->pcb_ext = 0;
296 	}
297 }
298 
299 void
300 cpu_sched_exit(td)
301 	register struct thread *td;
302 {
303 }
304 
305 void
306 cpu_thread_setup(struct thread *td)
307 {
308 
309 	td->td_pcb =
310 	     (struct pcb *)(td->td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
311 	td->td_frame = (struct trapframe *)((caddr_t)td->td_pcb - 16) - 1;
312 	td->td_pcb->pcb_ext = NULL;
313 }
314 
315 /*
316  * Initialize machine state (pcb and trap frame) for a new thread about to
317  * upcall. Pu t enough state in the new thread's PCB to get it to go back
318  * userret(), where we can intercept it again to set the return (upcall)
319  * Address and stack, along with those from upcals that are from other sources
320  * such as those generated in thread_userret() itself.
321  */
322 void
323 cpu_set_upcall(struct thread *td, void *pcb)
324 {
325 	struct pcb *pcb2;
326 
327 	/* Point the pcb to the top of the stack. */
328 	pcb2 = td->td_pcb;
329 
330 	/*
331 	 * Copy the upcall pcb.  This loads kernel regs.
332 	 * Those not loaded individually below get their default
333 	 * values here.
334 	 *
335 	 * XXXKSE It might be a good idea to simply skip this as
336 	 * the values of the other registers may be unimportant.
337 	 * This would remove any requirement for knowing the KSE
338 	 * at this time (see the matching comment below for
339 	 * more analysis) (need a good safe default).
340 	 */
341 	bcopy(pcb, pcb2, sizeof(*pcb2));
342 
343 	/*
344 	 * Create a new fresh stack for the new thread.
345 	 * The -16 is so we can expand the trapframe if we go to vm86.
346 	 * Don't forget to set this stack value into whatever supplies
347 	 * the address for the fault handlers.
348 	 * The contexts are filled in at the time we actually DO the
349 	 * upcall as only then do we know which KSE we got.
350 	 */
351 	td->td_frame = (struct trapframe *)((caddr_t)pcb2 - 16) - 1;
352 
353 	/*
354 	 * Set registers for trampoline to user mode.  Leave space for the
355 	 * return address on stack.  These are the kernel mode register values.
356 	 */
357 #ifdef PAE
358 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdpt);
359 #else
360 	pcb2->pcb_cr3 = vtophys(vmspace_pmap(td->td_proc->p_vmspace)->pm_pdir);
361 #endif
362 	pcb2->pcb_edi = 0;
363 	pcb2->pcb_esi = (int)fork_return;		    /* trampoline arg */
364 	pcb2->pcb_ebp = 0;
365 	pcb2->pcb_esp = (int)td->td_frame - sizeof(void *); /* trampoline arg */
366 	pcb2->pcb_ebx = (int)td;			    /* trampoline arg */
367 	pcb2->pcb_eip = (int)fork_trampoline;
368 	pcb2->pcb_psl &= ~(PSL_I);	/* interrupts must be disabled */
369 	pcb2->pcb_gs = rgs();
370 	/*
371 	 * If we didn't copy the pcb, we'd need to do the following registers:
372 	 * pcb2->pcb_dr*:	cloned above.
373 	 * pcb2->pcb_savefpu:	cloned above.
374 	 * pcb2->pcb_flags:	cloned above.
375 	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
376 	 * pcb2->pcb_gs:	cloned above.  XXXKSE ???
377 	 * pcb2->pcb_ext:	cleared below.
378 	 */
379 	 pcb2->pcb_ext = NULL;
380 }
381 
382 /*
383  * Set that machine state for performing an upcall that has to
384  * be done in thread_userret() so that those upcalls generated
385  * in thread_userret() itself can be done as well.
386  */
387 void
388 cpu_set_upcall_kse(struct thread *td, struct kse_upcall *ku)
389 {
390 
391 	/*
392 	 * Do any extra cleaning that needs to be done.
393 	 * The thread may have optional components
394 	 * that are not present in a fresh thread.
395 	 * This may be a recycled thread so make it look
396 	 * as though it's newly allocated.
397 	 */
398 	cpu_thread_clean(td);
399 
400 	/*
401 	 * Set the trap frame to point at the beginning of the uts
402 	 * function.
403 	 */
404 	td->td_frame->tf_esp =
405 	    (int)ku->ku_stack.ss_sp + ku->ku_stack.ss_size - 16;
406 	td->td_frame->tf_eip = (int)ku->ku_func;
407 
408 	/*
409 	 * Pass the address of the mailbox for this kse to the uts
410 	 * function as a parameter on the stack.
411 	 */
412 	suword((void *)(td->td_frame->tf_esp + sizeof(void *)),
413 	    (int)ku->ku_mailbox);
414 }
415 
416 void
417 cpu_wait(p)
418 	struct proc *p;
419 {
420 }
421 
422 /*
423  * Convert kernel VA to physical address
424  */
425 vm_paddr_t
426 kvtop(void *addr)
427 {
428 	vm_paddr_t pa;
429 
430 	pa = pmap_kextract((vm_offset_t)addr);
431 	if (pa == 0)
432 		panic("kvtop: zero page frame");
433 	return (pa);
434 }
435 
436 /*
437  * Force reset the processor by invalidating the entire address space!
438  */
439 
440 #ifdef SMP
441 static void
442 cpu_reset_proxy()
443 {
444 
445 	cpu_reset_proxy_active = 1;
446 	while (cpu_reset_proxy_active == 1)
447 		;	 /* Wait for other cpu to see that we've started */
448 	stop_cpus((1<<cpu_reset_proxyid));
449 	printf("cpu_reset_proxy: Stopped CPU %d\n", cpu_reset_proxyid);
450 	DELAY(1000000);
451 	cpu_reset_real();
452 }
453 #endif
454 
455 void
456 cpu_reset()
457 {
458 #ifdef SMP
459 	if (smp_active == 0) {
460 		cpu_reset_real();
461 		/* NOTREACHED */
462 	} else {
463 
464 		u_int map;
465 		int cnt;
466 		printf("cpu_reset called on cpu#%d\n", PCPU_GET(cpuid));
467 
468 		map = PCPU_GET(other_cpus) & ~ stopped_cpus;
469 
470 		if (map != 0) {
471 			printf("cpu_reset: Stopping other CPUs\n");
472 			stop_cpus(map);		/* Stop all other CPUs */
473 		}
474 
475 		if (PCPU_GET(cpuid) == 0) {
476 			DELAY(1000000);
477 			cpu_reset_real();
478 			/* NOTREACHED */
479 		} else {
480 			/* We are not BSP (CPU #0) */
481 
482 			cpu_reset_proxyid = PCPU_GET(cpuid);
483 			cpustop_restartfunc = cpu_reset_proxy;
484 			cpu_reset_proxy_active = 0;
485 			printf("cpu_reset: Restarting BSP\n");
486 			started_cpus = (1<<0);		/* Restart CPU #0 */
487 
488 			cnt = 0;
489 			while (cpu_reset_proxy_active == 0 && cnt < 10000000)
490 				cnt++;	/* Wait for BSP to announce restart */
491 			if (cpu_reset_proxy_active == 0)
492 				printf("cpu_reset: Failed to restart BSP\n");
493 			enable_intr();
494 			cpu_reset_proxy_active = 2;
495 
496 			while (1);
497 			/* NOTREACHED */
498 		}
499 	}
500 #else
501 	cpu_reset_real();
502 #endif
503 }
504 
505 static void
506 cpu_reset_real()
507 {
508 
509 #ifdef PC98
510 	/*
511 	 * Attempt to do a CPU reset via CPU reset port.
512 	 */
513 	disable_intr();
514 	if ((inb(0x35) & 0xa0) != 0xa0) {
515 		outb(0x37, 0x0f);		/* SHUT0 = 0. */
516 		outb(0x37, 0x0b);		/* SHUT1 = 0. */
517 	}
518 	outb(0xf0, 0x00);		/* Reset. */
519 #else
520 	/*
521 	 * Attempt to do a CPU reset via the keyboard controller,
522 	 * do not turn of the GateA20, as any machine that fails
523 	 * to do the reset here would then end up in no man's land.
524 	 */
525 
526 #if !defined(BROKEN_KEYBOARD_RESET)
527 	outb(IO_KBD + 4, 0xFE);
528 	DELAY(500000);	/* wait 0.5 sec to see if that did it */
529 	printf("Keyboard reset did not work, attempting CPU shutdown\n");
530 	DELAY(1000000);	/* wait 1 sec for printf to complete */
531 #endif
532 #endif /* PC98 */
533 	/* force a shutdown by unmapping entire address space ! */
534 	bzero((caddr_t)PTD, NBPTD);
535 
536 	/* "good night, sweet prince .... <THUNK!>" */
537 	invltlb();
538 	/* NOTREACHED */
539 	while(1);
540 }
541 
542 /*
543  * Software interrupt handler for queued VM system processing.
544  */
545 void
546 swi_vm(void *dummy)
547 {
548 	if (busdma_swi_pending != 0)
549 		busdma_swi();
550 }
551 
552 /*
553  * Tell whether this address is in some physical memory region.
554  * Currently used by the kernel coredump code in order to avoid
555  * dumping the ``ISA memory hole'' which could cause indefinite hangs,
556  * or other unpredictable behaviour.
557  */
558 
559 int
560 is_physical_memory(addr)
561 	vm_offset_t addr;
562 {
563 
564 #ifdef DEV_ISA
565 	/* The ISA ``memory hole''. */
566 	if (addr >= 0xa0000 && addr < 0x100000)
567 		return 0;
568 #endif
569 
570 	/*
571 	 * stuff other tests for known memory-mapped devices (PCI?)
572 	 * here
573 	 */
574 
575 	return 1;
576 }
577