vm_machdep.c revision 82309
1139825Simp/*-
21541Srgrimes * Copyright (c) 1982, 1986 The Regents of the University of California.
31541Srgrimes * Copyright (c) 1989, 1990 William Jolitz
41541Srgrimes * Copyright (c) 1994 John Dyson
51541Srgrimes * All rights reserved.
61541Srgrimes *
71541Srgrimes * This code is derived from software contributed to Berkeley by
81541Srgrimes * the Systems Programming Group of the University of Utah Computer
91541Srgrimes * Science Department, and William Jolitz.
101541Srgrimes *
111541Srgrimes * Redistribution and use in source and binary forms, with or without
121541Srgrimes * modification, are permitted provided that the following conditions
131541Srgrimes * are met:
141541Srgrimes * 1. Redistributions of source code must retain the above copyright
151541Srgrimes *    notice, this list of conditions and the following disclaimer.
161541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
171541Srgrimes *    notice, this list of conditions and the following disclaimer in the
181541Srgrimes *    documentation and/or other materials provided with the distribution.
191541Srgrimes * 3. All advertising materials mentioning features or use of this software
201541Srgrimes *    must display the following acknowledgement:
211541Srgrimes *	This product includes software developed by the University of
221541Srgrimes *	California, Berkeley and its contributors.
231541Srgrimes * 4. Neither the name of the University nor the names of its contributors
241541Srgrimes *    may be used to endorse or promote products derived from this software
251541Srgrimes *    without specific prior written permission.
261541Srgrimes *
271541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
281541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
291541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3050477Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
311541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
321541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
331541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
341541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
351541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36103731Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3734319Sdufault * SUCH DAMAGE.
38103731Swollman *
39103731Swollman *	from: @(#)vm_machdep.c	7.3 (Berkeley) 5/13/91
40103731Swollman *	Utah $Hdr: vm_machdep.c 1.16.1.1 89/06/23$
41103731Swollman * $FreeBSD: head/sys/amd64/amd64/vm_machdep.c 82309 2001-08-25 02:20:02Z peter $
42103731Swollman */
43103731Swollman
44103731Swollman#include "opt_npx.h"
45103731Swollman#ifdef PC98
46103731Swollman#include "opt_pc98.h"
47103731Swollman#endif
48103731Swollman#include "opt_reset.h"
49103731Swollman#include "opt_isa.h"
50103731Swollman#include "opt_upages.h"
51103731Swollman
52103731Swollman#include <sys/param.h>
53103731Swollman#include <sys/systm.h>
54103731Swollman#include <sys/malloc.h>
55103731Swollman#include <sys/proc.h>
56103731Swollman#include <sys/bio.h>
57103731Swollman#include <sys/buf.h>
58106055Swollman#include <sys/vnode.h>
59103731Swollman#include <sys/vmmeter.h>
60103731Swollman#include <sys/kernel.h>
61103731Swollman#include <sys/ktr.h>
62103731Swollman#include <sys/mutex.h>
63103731Swollman#include <sys/smp.h>
64153006Sdavidxu#include <sys/sysctl.h>
65151659Swollman#include <sys/unistd.h>
66103731Swollman
67103731Swollman#include <machine/cpu.h>
68103731Swollman#include <machine/md_var.h>
69103731Swollman#include <machine/pcb.h>
70151578Sdavidxu#include <machine/pcb_ext.h>
71103731Swollman#include <machine/vm86.h>
72103731Swollman
73103731Swollman#include <vm/vm.h>
74103731Swollman#include <vm/vm_param.h>
75153004Sdavidxu#include <sys/lock.h>
76151872Sdavidxu#include <vm/vm_kern.h>
77103731Swollman#include <vm/vm_page.h>
78103731Swollman#include <vm/vm_map.h>
791541Srgrimes#include <vm/vm_extern.h>
80103731Swollman
81103731Swollman#include <sys/user.h>
82103731Swollman
83103731Swollman#ifdef PC98
84103731Swollman#include <pc98/pc98/pc98.h>
851541Srgrimes#else
8620640Sbde#include <i386/isa/isa.h>
8720640Sbde#endif
8820640Sbde
89103731Swollmanstatic void	cpu_reset_real __P((void));
90103731Swollman#ifdef SMP
911541Srgrimesstatic void	cpu_reset_proxy __P((void));
9220640Sbdestatic u_int	cpu_reset_proxyid;
93103731Swollmanstatic volatile u_int	cpu_reset_proxy_active;
9420640Sbde#endif
951541Srgrimesextern int	_ucodesel, _udatasel;
96105045Smike
97105045Smike/*
98105045Smike * quick version of vm_fault
991541Srgrimes */
1001541Srgrimesint
1011541Srgrimesvm_fault_quick(v, prot)
1021541Srgrimes	caddr_t v;
1031541Srgrimes	int prot;
1041541Srgrimes{
1051541Srgrimes	int r;
106105789Sphk
1071541Srgrimes	if (prot & VM_PROT_WRITE)
1081541Srgrimes		r = subyte(v, fubyte(v));
1091541Srgrimes	else
110168397Spjd		r = fubyte(v);
111168397Spjd	return(r);
112105789Sphk}
1131541Srgrimes
1141541Srgrimes/*
1151541Srgrimes * Finish a fork operation, with process p2 nearly set up.
1161541Srgrimes * Copy and update the pcb, set up the stack so that the child
1171541Srgrimes * ready to run and return to user mode.
1181541Srgrimes */
1191541Srgrimesvoid
1201541Srgrimescpu_fork(p1, p2, flags)
1211541Srgrimes	register struct proc *p1, *p2;
1221541Srgrimes	int flags;
1231541Srgrimes{
1241541Srgrimes	struct pcb *pcb2;
1251541Srgrimes#ifdef DEV_NPX
1261541Srgrimes	int savecrit;
1271541Srgrimes#endif
1281541Srgrimes
1291541Srgrimes	if ((flags & RFPROC) == 0) {
1301541Srgrimes		if ((flags & RFMEM) == 0) {
1311541Srgrimes			/* unshare user LDT */
132103731Swollman			struct pcb *pcb1 = &p1->p_addr->u_pcb;
133103768Sbde			struct pcb_ldt *pcb_ldt = pcb1->pcb_ldt;
134103768Sbde			if (pcb_ldt && pcb_ldt->ldt_refcnt > 1) {
135103768Sbde				pcb_ldt = user_ldt_alloc(pcb1,pcb_ldt->ldt_len);
136103731Swollman				if (pcb_ldt == NULL)
13734030Sdufault					panic("could not copy LDT");
138103731Swollman				pcb1->pcb_ldt = pcb_ldt;
139103731Swollman				set_user_ldt(pcb1);
140103731Swollman				user_ldt_free(pcb1);
141103731Swollman			}
142103731Swollman		}
143103731Swollman		return;
144103731Swollman	}
145106055Swollman
146103731Swollman	/* Ensure that p1's pcb is up to date. */
14734030Sdufault#ifdef DEV_NPX
148103731Swollman	if (p1 == curproc)
149105572Srwatson		p1->p_addr->u_pcb.pcb_gs = rgs();
150105572Srwatson	savecrit = critical_enter();
151105572Srwatson	if (PCPU_GET(npxproc) == p1)
152105572Srwatson		npxsave(&p1->p_addr->u_pcb.pcb_save);
153105572Srwatson	critical_exit(savecrit);
154103731Swollman#endif
155101071Srwatson
156168397Spjd	/* Copy p1's pcb. */
157168397Spjd	p2->p_addr->u_pcb = p1->p_addr->u_pcb;
158168397Spjd	pcb2 = &p2->p_addr->u_pcb;
159103731Swollman
16014222Speter	/*
16114222Speter	 * Create a new fresh stack for the new process.
16214222Speter	 * Copy the trap frame for the return to user mode as if from a
16365557Sjasone	 * syscall.  This copies most of the user mode register values.
16414222Speter	 */
165103768Sbde	p2->p_frame = (struct trapframe *)
166103768Sbde			   ((int)p2->p_addr + UPAGES * PAGE_SIZE - 16) - 1;
167103768Sbde	bcopy(p1->p_frame, p2->p_frame, sizeof(struct trapframe));
168103768Sbde
169103768Sbde	p2->p_frame->tf_eax = 0;		/* Child returns zero */
170103768Sbde	p2->p_frame->tf_eflags &= ~PSL_C;	/* success */
171103768Sbde	p2->p_frame->tf_edx = 1;
172103768Sbde
173103768Sbde	/*
174103768Sbde	 * Set registers for trampoline to user mode.  Leave space for the
175103731Swollman	 * return address on stack.  These are the kernel mode register values.
176103731Swollman	 */
177103768Sbde	pcb2->pcb_cr3 = vtophys(vmspace_pmap(p2->p_vmspace)->pm_pdir);
178103731Swollman	pcb2->pcb_edi = 0;
179103768Sbde	pcb2->pcb_esi = (int)fork_return;	/* fork_trampoline argument */
180103768Sbde	pcb2->pcb_ebp = 0;
181103768Sbde	pcb2->pcb_esp = (int)p2->p_frame - sizeof(void *);
18226671Sdyson	pcb2->pcb_ebx = (int)p2;		/* fork_trampoline argument */
183103731Swollman	pcb2->pcb_eip = (int)fork_trampoline;
18414222Speter	/*-
1851541Srgrimes	 * pcb2->pcb_dr*:	cloned above.
186	 * pcb2->pcb_ldt:	duplicated below, if necessary.
187	 * pcb2->pcb_savefpu:	cloned above.
188	 * pcb2->pcb_flags:	cloned above.
189	 * pcb2->pcb_onfault:	cloned above (always NULL here?).
190	 * pcb2->pcb_gs:	cloned above.
191	 * pcb2->pcb_ext:	cleared below.
192	 */
193
194	/*
195	 * XXX don't copy the i/o pages.  this should probably be fixed.
196	 */
197	pcb2->pcb_ext = 0;
198
199        /* Copy the LDT, if necessary. */
200	mtx_lock_spin(&sched_lock);
201        if (pcb2->pcb_ldt != 0) {
202		if (flags & RFMEM) {
203			pcb2->pcb_ldt->ldt_refcnt++;
204		} else {
205			pcb2->pcb_ldt = user_ldt_alloc(pcb2,
206				pcb2->pcb_ldt->ldt_len);
207			if (pcb2->pcb_ldt == NULL)
208				panic("could not copy LDT");
209		}
210        }
211	mtx_unlock_spin(&sched_lock);
212
213	/*
214	 * Now, cpu_switch() can schedule the new process.
215	 * pcb_esp is loaded pointing to the cpu_switch() stack frame
216	 * containing the return address when exiting cpu_switch.
217	 * This will normally be to fork_trampoline(), which will have
218	 * %ebx loaded with the new proc's pointer.  fork_trampoline()
219	 * will set up a stack to call fork_return(p, frame); to complete
220	 * the return to user-mode.
221	 */
222}
223
224/*
225 * Intercept the return address from a freshly forked process that has NOT
226 * been scheduled yet.
227 *
228 * This is needed to make kernel threads stay in kernel mode.
229 */
230void
231cpu_set_fork_handler(p, func, arg)
232	struct proc *p;
233	void (*func) __P((void *));
234	void *arg;
235{
236	/*
237	 * Note that the trap frame follows the args, so the function
238	 * is really called like this:  func(arg, frame);
239	 */
240	p->p_addr->u_pcb.pcb_esi = (int) func;	/* function */
241	p->p_addr->u_pcb.pcb_ebx = (int) arg;	/* first arg */
242}
243
244void
245cpu_exit(p)
246	register struct proc *p;
247{
248	struct pcb *pcb = &p->p_addr->u_pcb;
249
250#ifdef DEV_NPX
251	npxexit(p);
252#endif
253	if (pcb->pcb_ext != 0) {
254	        /*
255		 * XXX do we need to move the TSS off the allocated pages
256		 * before freeing them?  (not done here)
257		 */
258		kmem_free(kernel_map, (vm_offset_t)pcb->pcb_ext,
259		    ctob(IOPAGES + 1));
260		pcb->pcb_ext = 0;
261	}
262	if (pcb->pcb_ldt)
263		user_ldt_free(pcb);
264        if (pcb->pcb_flags & PCB_DBREGS) {
265                /*
266                 * disable all hardware breakpoints
267                 */
268                reset_dbregs();
269                pcb->pcb_flags &= ~PCB_DBREGS;
270        }
271	PROC_LOCK(p);
272	mtx_lock_spin(&sched_lock);
273	while (mtx_owned(&Giant))
274		mtx_unlock_flags(&Giant, MTX_NOSWITCH);
275
276	/*
277	 * We have to wait until after releasing all locks before
278	 * changing p_stat.  If we block on a mutex then we will be
279	 * back at SRUN when we resume and our parent will never
280	 * harvest us.
281	 */
282	p->p_stat = SZOMB;
283
284	wakeup(p->p_pptr);
285	PROC_UNLOCK_NOSWITCH(p);
286
287	cnt.v_swtch++;
288	cpu_throw();
289	panic("cpu_exit");
290}
291
292void
293cpu_wait(p)
294	struct proc *p;
295{
296	GIANT_REQUIRED;
297
298	/* drop per-process resources */
299	pmap_dispose_proc(p);
300
301	/* and clean-out the vmspace */
302	vmspace_free(p->p_vmspace);
303}
304
305/*
306 * Dump the machine specific header information at the start of a core dump.
307 */
308int
309cpu_coredump(p, vp, cred)
310	struct proc *p;
311	struct vnode *vp;
312	struct ucred *cred;
313{
314	int error;
315	caddr_t tempuser;
316
317	tempuser = malloc(ctob(UPAGES), M_TEMP, M_WAITOK | M_ZERO);
318	if (!tempuser)
319		return EINVAL;
320
321	bcopy(p->p_addr, tempuser, sizeof(struct user));
322	bcopy(p->p_frame,
323	      tempuser + ((caddr_t) p->p_frame - (caddr_t) p->p_addr),
324	      sizeof(struct trapframe));
325
326	error = vn_rdwr(UIO_WRITE, vp, (caddr_t) tempuser,
327			ctob(UPAGES),
328			(off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
329			cred, (int *)NULL, p);
330
331	free(tempuser, M_TEMP);
332
333	return error;
334}
335
336#ifdef notyet
337static void
338setredzone(pte, vaddr)
339	u_short *pte;
340	caddr_t vaddr;
341{
342/* eventually do this by setting up an expand-down stack segment
343   for ss0: selector, allowing stack access down to top of u.
344   this means though that protection violations need to be handled
345   thru a double fault exception that must do an integral task
346   switch to a known good context, within which a dump can be
347   taken. a sensible scheme might be to save the initial context
348   used by sched (that has physical memory mapped 1:1 at bottom)
349   and take the dump while still in mapped mode */
350}
351#endif
352
353/*
354 * Convert kernel VA to physical address
355 */
356u_long
357kvtop(void *addr)
358{
359	vm_offset_t va;
360
361	va = pmap_kextract((vm_offset_t)addr);
362	if (va == 0)
363		panic("kvtop: zero page frame");
364	return((int)va);
365}
366
367/*
368 * Map an IO request into kernel virtual address space.
369 *
370 * All requests are (re)mapped into kernel VA space.
371 * Notice that we use b_bufsize for the size of the buffer
372 * to be mapped.  b_bcount might be modified by the driver.
373 */
374void
375vmapbuf(bp)
376	register struct buf *bp;
377{
378	register caddr_t addr, v, kva;
379	vm_offset_t pa;
380
381	GIANT_REQUIRED;
382
383	if ((bp->b_flags & B_PHYS) == 0)
384		panic("vmapbuf");
385
386	for (v = bp->b_saveaddr, addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data);
387	    addr < bp->b_data + bp->b_bufsize;
388	    addr += PAGE_SIZE, v += PAGE_SIZE) {
389		/*
390		 * Do the vm_fault if needed; do the copy-on-write thing
391		 * when reading stuff off device into memory.
392		 */
393		vm_fault_quick(addr,
394			(bp->b_iocmd == BIO_READ)?(VM_PROT_READ|VM_PROT_WRITE):VM_PROT_READ);
395		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
396		if (pa == 0)
397			panic("vmapbuf: page not present");
398		vm_page_hold(PHYS_TO_VM_PAGE(pa));
399		pmap_kenter((vm_offset_t) v, pa);
400	}
401
402	kva = bp->b_saveaddr;
403	bp->b_saveaddr = bp->b_data;
404	bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
405}
406
407/*
408 * Free the io map PTEs associated with this IO operation.
409 * We also invalidate the TLB entries and restore the original b_addr.
410 */
411void
412vunmapbuf(bp)
413	register struct buf *bp;
414{
415	register caddr_t addr;
416	vm_offset_t pa;
417
418	GIANT_REQUIRED;
419
420	if ((bp->b_flags & B_PHYS) == 0)
421		panic("vunmapbuf");
422
423	for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data);
424	    addr < bp->b_data + bp->b_bufsize;
425	    addr += PAGE_SIZE) {
426		pa = trunc_page(pmap_kextract((vm_offset_t) addr));
427		pmap_kremove((vm_offset_t) addr);
428		vm_page_unhold(PHYS_TO_VM_PAGE(pa));
429	}
430
431	bp->b_data = bp->b_saveaddr;
432}
433
434/*
435 * Force reset the processor by invalidating the entire address space!
436 */
437
438#ifdef SMP
439static void
440cpu_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
453void
454cpu_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
503static void
504cpu_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, PAGE_SIZE);
533
534	/* "good night, sweet prince .... <THUNK!>" */
535	invltlb();
536	/* NOTREACHED */
537	while(1);
538}
539
540int
541grow_stack(p, sp)
542	struct proc *p;
543	u_int sp;
544{
545	int rv;
546
547	rv = vm_map_growstack (p, sp);
548	if (rv != KERN_SUCCESS)
549		return (0);
550
551	return (1);
552}
553
554/*
555 * Software interrupt handler for queued VM system processing.
556 */
557void
558swi_vm(void *dummy)
559{
560	if (busdma_swi_pending != 0)
561		busdma_swi();
562}
563
564/*
565 * Tell whether this address is in some physical memory region.
566 * Currently used by the kernel coredump code in order to avoid
567 * dumping the ``ISA memory hole'' which could cause indefinite hangs,
568 * or other unpredictable behaviour.
569 */
570
571int
572is_physical_memory(addr)
573	vm_offset_t addr;
574{
575
576#ifdef DEV_ISA
577	/* The ISA ``memory hole''. */
578	if (addr >= 0xa0000 && addr < 0x100000)
579		return 0;
580#endif
581
582	/*
583	 * stuff other tests for known memory-mapped devices (PCI?)
584	 * here
585	 */
586
587	return 1;
588}
589