xref: /minix3/minix/kernel/arch/i386/memory.c (revision 9624407e7addfd8b88486acfe3a0e056e2b92ee3)
1433d6423SLionel Sambuc 
2433d6423SLionel Sambuc #include "kernel/kernel.h"
3433d6423SLionel Sambuc #include "kernel/vm.h"
4433d6423SLionel Sambuc 
5433d6423SLionel Sambuc #include <machine/vm.h>
6433d6423SLionel Sambuc 
7433d6423SLionel Sambuc #include <minix/syslib.h>
8433d6423SLionel Sambuc #include <minix/cpufeature.h>
9433d6423SLionel Sambuc #include <string.h>
10433d6423SLionel Sambuc #include <assert.h>
11433d6423SLionel Sambuc #include <signal.h>
12433d6423SLionel Sambuc #include <stdlib.h>
13433d6423SLionel Sambuc 
14433d6423SLionel Sambuc #include <machine/vm.h>
15433d6423SLionel Sambuc 
16433d6423SLionel Sambuc #include "oxpcie.h"
17433d6423SLionel Sambuc #include "arch_proto.h"
18433d6423SLionel Sambuc 
19433d6423SLionel Sambuc #ifdef USE_APIC
20433d6423SLionel Sambuc #include "apic.h"
21433d6423SLionel Sambuc #ifdef USE_WATCHDOG
22433d6423SLionel Sambuc #include "kernel/watchdog.h"
23433d6423SLionel Sambuc #endif
24433d6423SLionel Sambuc #endif
25433d6423SLionel Sambuc 
26433d6423SLionel Sambuc phys_bytes video_mem_vaddr = 0;
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc #define HASPT(procptr) ((procptr)->p_seg.p_cr3 != 0)
29433d6423SLionel Sambuc static int nfreepdes = 0;
30433d6423SLionel Sambuc #define MAXFREEPDES	2
31433d6423SLionel Sambuc static int freepdes[MAXFREEPDES];
32433d6423SLionel Sambuc 
33433d6423SLionel Sambuc static u32_t phys_get32(phys_bytes v);
34433d6423SLionel Sambuc 
mem_clear_mapcache(void)35433d6423SLionel Sambuc void mem_clear_mapcache(void)
36433d6423SLionel Sambuc {
37433d6423SLionel Sambuc 	int i;
38433d6423SLionel Sambuc 	for(i = 0; i < nfreepdes; i++) {
39433d6423SLionel Sambuc 		struct proc *ptproc = get_cpulocal_var(ptproc);
40433d6423SLionel Sambuc 		int pde = freepdes[i];
41433d6423SLionel Sambuc 		u32_t *ptv;
42433d6423SLionel Sambuc 		assert(ptproc);
43433d6423SLionel Sambuc 		ptv = ptproc->p_seg.p_cr3_v;
44433d6423SLionel Sambuc 		assert(ptv);
45433d6423SLionel Sambuc 		ptv[pde] = 0;
46433d6423SLionel Sambuc 	}
47433d6423SLionel Sambuc }
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc /* This function sets up a mapping from within the kernel's address
50433d6423SLionel Sambuc  * space to any other area of memory, either straight physical
51433d6423SLionel Sambuc  * memory (pr == NULL) or a process view of memory, in 4MB windows.
52433d6423SLionel Sambuc  * I.e., it maps in 4MB chunks of virtual (or physical) address space
53433d6423SLionel Sambuc  * to 4MB chunks of kernel virtual address space.
54433d6423SLionel Sambuc  *
55433d6423SLionel Sambuc  * It recognizes pr already being in memory as a special case (no
56433d6423SLionel Sambuc  * mapping required).
57433d6423SLionel Sambuc  *
58433d6423SLionel Sambuc  * The target (i.e. in-kernel) mapping area is one of the freepdes[]
59433d6423SLionel Sambuc  * VM has earlier already told the kernel about that is available. It is
60433d6423SLionel Sambuc  * identified as the 'pde' parameter. This value can be chosen freely
61433d6423SLionel Sambuc  * by the caller, as long as it is in range (i.e. 0 or higher and corresponds
62433d6423SLionel Sambuc  * to a known freepde slot). It is up to the caller to keep track of which
63433d6423SLionel Sambuc  * freepde's are in use, and to determine which ones are free to use.
64433d6423SLionel Sambuc  *
65433d6423SLionel Sambuc  * The logical number supplied by the caller is translated into an actual
66433d6423SLionel Sambuc  * pde number to be used, and a pointer to it (linear address) is returned
67433d6423SLionel Sambuc  * for actual use by phys_copy or memset.
68433d6423SLionel Sambuc  */
createpde(const struct proc * pr,const phys_bytes linaddr,phys_bytes * bytes,int free_pde_idx,int * changed)69433d6423SLionel Sambuc static phys_bytes createpde(
70433d6423SLionel Sambuc 	const struct proc *pr,	/* Requested process, NULL for physical. */
71433d6423SLionel Sambuc 	const phys_bytes linaddr,/* Address after segment translation. */
72433d6423SLionel Sambuc 	phys_bytes *bytes,	/* Size of chunk, function may truncate it. */
73433d6423SLionel Sambuc 	int free_pde_idx,	/* index of the free slot to use */
74433d6423SLionel Sambuc 	int *changed		/* If mapping is made, this is set to 1. */
75433d6423SLionel Sambuc 	)
76433d6423SLionel Sambuc {
77433d6423SLionel Sambuc 	u32_t pdeval;
78433d6423SLionel Sambuc 	phys_bytes offset;
79433d6423SLionel Sambuc 	int pde;
80433d6423SLionel Sambuc 
81433d6423SLionel Sambuc 	assert(free_pde_idx >= 0 && free_pde_idx < nfreepdes);
82433d6423SLionel Sambuc 	pde = freepdes[free_pde_idx];
83433d6423SLionel Sambuc 	assert(pde >= 0 && pde < 1024);
84433d6423SLionel Sambuc 
85433d6423SLionel Sambuc 	if(pr && ((pr == get_cpulocal_var(ptproc)) || iskernelp(pr))) {
86433d6423SLionel Sambuc 		/* Process memory is requested, and
87433d6423SLionel Sambuc 		 * it's a process that is already in current page table, or
88433d6423SLionel Sambuc 		 * the kernel, which is always there.
89433d6423SLionel Sambuc 		 * Therefore linaddr is valid directly, with the requested
90433d6423SLionel Sambuc 		 * size.
91433d6423SLionel Sambuc 		 */
92433d6423SLionel Sambuc 		return linaddr;
93433d6423SLionel Sambuc 	}
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc 	if(pr) {
96433d6423SLionel Sambuc 		/* Requested address is in a process that is not currently
97433d6423SLionel Sambuc 		 * accessible directly. Grab the PDE entry of that process'
98433d6423SLionel Sambuc 		 * page table that corresponds to the requested address.
99433d6423SLionel Sambuc 		 */
100433d6423SLionel Sambuc 		assert(pr->p_seg.p_cr3_v);
101433d6423SLionel Sambuc 		pdeval = pr->p_seg.p_cr3_v[I386_VM_PDE(linaddr)];
102433d6423SLionel Sambuc 	} else {
103433d6423SLionel Sambuc 		/* Requested address is physical. Make up the PDE entry. */
104433d6423SLionel Sambuc 		pdeval = (linaddr & I386_VM_ADDR_MASK_4MB) |
105433d6423SLionel Sambuc 			I386_VM_BIGPAGE | I386_VM_PRESENT |
106433d6423SLionel Sambuc 			I386_VM_WRITE | I386_VM_USER;
107433d6423SLionel Sambuc 	}
108433d6423SLionel Sambuc 
109433d6423SLionel Sambuc 	/* Write the pde value that we need into a pde that the kernel
110433d6423SLionel Sambuc 	 * can access, into the currently loaded page table so it becomes
111433d6423SLionel Sambuc 	 * visible.
112433d6423SLionel Sambuc 	 */
113433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
114433d6423SLionel Sambuc 	if(get_cpulocal_var(ptproc)->p_seg.p_cr3_v[pde] != pdeval) {
115433d6423SLionel Sambuc 		get_cpulocal_var(ptproc)->p_seg.p_cr3_v[pde] = pdeval;
116433d6423SLionel Sambuc 		*changed = 1;
117433d6423SLionel Sambuc 	}
118433d6423SLionel Sambuc 
119433d6423SLionel Sambuc 	/* Memory is now available, but only the 4MB window of virtual
120433d6423SLionel Sambuc 	 * address space that we have mapped; calculate how much of
121433d6423SLionel Sambuc 	 * the requested range is visible and return that in *bytes,
122433d6423SLionel Sambuc 	 * if that is less than the requested range.
123433d6423SLionel Sambuc 	 */
124433d6423SLionel Sambuc 	offset = linaddr & I386_VM_OFFSET_MASK_4MB; /* Offset in 4MB window. */
125433d6423SLionel Sambuc 	*bytes = MIN(*bytes, I386_BIG_PAGE_SIZE - offset);
126433d6423SLionel Sambuc 
127433d6423SLionel Sambuc 	/* Return the linear address of the start of the new mapping. */
128433d6423SLionel Sambuc 	return I386_BIG_PAGE_SIZE*pde + offset;
129433d6423SLionel Sambuc }
130433d6423SLionel Sambuc 
131433d6423SLionel Sambuc 
132433d6423SLionel Sambuc /*===========================================================================*
133433d6423SLionel Sambuc  *                           check_resumed_caller                            *
134433d6423SLionel Sambuc  *===========================================================================*/
check_resumed_caller(struct proc * caller)135433d6423SLionel Sambuc static int check_resumed_caller(struct proc *caller)
136433d6423SLionel Sambuc {
137433d6423SLionel Sambuc 	/* Returns the result from VM if caller was resumed, otherwise OK. */
138433d6423SLionel Sambuc 	if (caller && (caller->p_misc_flags & MF_KCALL_RESUME)) {
139433d6423SLionel Sambuc 		assert(caller->p_vmrequest.vmresult != VMSUSPEND);
140433d6423SLionel Sambuc 		return caller->p_vmrequest.vmresult;
141433d6423SLionel Sambuc 	}
142433d6423SLionel Sambuc 
143433d6423SLionel Sambuc 	return OK;
144433d6423SLionel Sambuc }
145433d6423SLionel Sambuc 
146433d6423SLionel Sambuc /*===========================================================================*
147433d6423SLionel Sambuc  *				lin_lin_copy				     *
148433d6423SLionel Sambuc  *===========================================================================*/
lin_lin_copy(struct proc * srcproc,vir_bytes srclinaddr,struct proc * dstproc,vir_bytes dstlinaddr,vir_bytes bytes)149433d6423SLionel Sambuc static int lin_lin_copy(struct proc *srcproc, vir_bytes srclinaddr,
150433d6423SLionel Sambuc 	struct proc *dstproc, vir_bytes dstlinaddr, vir_bytes bytes)
151433d6423SLionel Sambuc {
152433d6423SLionel Sambuc 	u32_t addr;
153433d6423SLionel Sambuc 	proc_nr_t procslot;
154433d6423SLionel Sambuc 
155433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc));
156433d6423SLionel Sambuc 	assert(get_cpulocal_var(proc_ptr));
157433d6423SLionel Sambuc 	assert(read_cr3() == get_cpulocal_var(ptproc)->p_seg.p_cr3);
158433d6423SLionel Sambuc 
159433d6423SLionel Sambuc 	procslot = get_cpulocal_var(ptproc)->p_nr;
160433d6423SLionel Sambuc 
161433d6423SLionel Sambuc 	assert(procslot >= 0 && procslot < I386_VM_DIR_ENTRIES);
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
164433d6423SLionel Sambuc 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
165433d6423SLionel Sambuc 	assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
166433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
167433d6423SLionel Sambuc 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_VMINHIBIT));
168433d6423SLionel Sambuc 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_VMINHIBIT));
169433d6423SLionel Sambuc 
170433d6423SLionel Sambuc 	while(bytes > 0) {
171433d6423SLionel Sambuc 		phys_bytes srcptr, dstptr;
172433d6423SLionel Sambuc 		vir_bytes chunk = bytes;
173433d6423SLionel Sambuc 		int changed = 0;
174433d6423SLionel Sambuc 
175433d6423SLionel Sambuc #ifdef CONFIG_SMP
176433d6423SLionel Sambuc 		unsigned cpu = cpuid;
177433d6423SLionel Sambuc 
178433d6423SLionel Sambuc 		if (srcproc && GET_BIT(srcproc->p_stale_tlb, cpu)) {
179433d6423SLionel Sambuc 			changed = 1;
180433d6423SLionel Sambuc 			UNSET_BIT(srcproc->p_stale_tlb, cpu);
181433d6423SLionel Sambuc 		}
182433d6423SLionel Sambuc 		if (dstproc && GET_BIT(dstproc->p_stale_tlb, cpu)) {
183433d6423SLionel Sambuc 			changed = 1;
184433d6423SLionel Sambuc 			UNSET_BIT(dstproc->p_stale_tlb, cpu);
185433d6423SLionel Sambuc 		}
186433d6423SLionel Sambuc #endif
187433d6423SLionel Sambuc 
188433d6423SLionel Sambuc 		/* Set up 4MB ranges. */
189433d6423SLionel Sambuc 		srcptr = createpde(srcproc, srclinaddr, &chunk, 0, &changed);
190433d6423SLionel Sambuc 		dstptr = createpde(dstproc, dstlinaddr, &chunk, 1, &changed);
191433d6423SLionel Sambuc 		if(changed)
192433d6423SLionel Sambuc 			reload_cr3();
193433d6423SLionel Sambuc 
194d09f72c4SDavid van Moolenbroek 		/* Check for overflow. */
195d09f72c4SDavid van Moolenbroek 		if (srcptr + chunk < srcptr) return EFAULT_SRC;
196d09f72c4SDavid van Moolenbroek 		if (dstptr + chunk < dstptr) return EFAULT_DST;
197d09f72c4SDavid van Moolenbroek 
198433d6423SLionel Sambuc 		/* Copy pages. */
199433d6423SLionel Sambuc 		PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr);
200433d6423SLionel Sambuc 
201433d6423SLionel Sambuc 		if(addr) {
202433d6423SLionel Sambuc 			/* If addr is nonzero, a page fault was caught. */
203433d6423SLionel Sambuc 
204433d6423SLionel Sambuc 			if(addr >= srcptr && addr < (srcptr + chunk)) {
205433d6423SLionel Sambuc 				return EFAULT_SRC;
206433d6423SLionel Sambuc 			}
207433d6423SLionel Sambuc 			if(addr >= dstptr && addr < (dstptr + chunk)) {
208433d6423SLionel Sambuc 				return EFAULT_DST;
209433d6423SLionel Sambuc 			}
210433d6423SLionel Sambuc 
211433d6423SLionel Sambuc 			panic("lin_lin_copy fault out of range");
212433d6423SLionel Sambuc 
213433d6423SLionel Sambuc 			/* Not reached. */
214433d6423SLionel Sambuc 			return EFAULT;
215433d6423SLionel Sambuc 		}
216433d6423SLionel Sambuc 
217433d6423SLionel Sambuc 		/* Update counter and addresses for next iteration, if any. */
218433d6423SLionel Sambuc 		bytes -= chunk;
219433d6423SLionel Sambuc 		srclinaddr += chunk;
220433d6423SLionel Sambuc 		dstlinaddr += chunk;
221433d6423SLionel Sambuc 	}
222433d6423SLionel Sambuc 
223433d6423SLionel Sambuc 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
224433d6423SLionel Sambuc 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
225433d6423SLionel Sambuc 	assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
226433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
227433d6423SLionel Sambuc 
228433d6423SLionel Sambuc 	return OK;
229433d6423SLionel Sambuc }
230433d6423SLionel Sambuc 
231433d6423SLionel Sambuc 
phys_get32(phys_bytes addr)232433d6423SLionel Sambuc static u32_t phys_get32(phys_bytes addr)
233433d6423SLionel Sambuc {
234433d6423SLionel Sambuc 	u32_t v;
235433d6423SLionel Sambuc 	int r;
236433d6423SLionel Sambuc 
237433d6423SLionel Sambuc 	if((r=lin_lin_copy(NULL, addr,
238433d6423SLionel Sambuc 		proc_addr(SYSTEM), (phys_bytes) &v, sizeof(v))) != OK) {
239433d6423SLionel Sambuc 		panic("lin_lin_copy for phys_get32 failed: %d",  r);
240433d6423SLionel Sambuc 	}
241433d6423SLionel Sambuc 
242433d6423SLionel Sambuc 	return v;
243433d6423SLionel Sambuc }
244433d6423SLionel Sambuc 
245433d6423SLionel Sambuc #if 0
246433d6423SLionel Sambuc static char *cr0_str(u32_t e)
247433d6423SLionel Sambuc {
248433d6423SLionel Sambuc 	static char str[80];
249433d6423SLionel Sambuc 	strcpy(str, "");
250433d6423SLionel Sambuc #define FLAG(v) do { if(e & (v)) { strcat(str, #v " "); e &= ~v; } } while(0)
251433d6423SLionel Sambuc 	FLAG(I386_CR0_PE);
252433d6423SLionel Sambuc 	FLAG(I386_CR0_MP);
253433d6423SLionel Sambuc 	FLAG(I386_CR0_EM);
254433d6423SLionel Sambuc 	FLAG(I386_CR0_TS);
255433d6423SLionel Sambuc 	FLAG(I386_CR0_ET);
256433d6423SLionel Sambuc 	FLAG(I386_CR0_PG);
257433d6423SLionel Sambuc 	FLAG(I386_CR0_WP);
258433d6423SLionel Sambuc 	if(e) { strcat(str, " (++)"); }
259433d6423SLionel Sambuc 	return str;
260433d6423SLionel Sambuc }
261433d6423SLionel Sambuc 
262433d6423SLionel Sambuc static char *cr4_str(u32_t e)
263433d6423SLionel Sambuc {
264433d6423SLionel Sambuc 	static char str[80];
265433d6423SLionel Sambuc 	strcpy(str, "");
266433d6423SLionel Sambuc 	FLAG(I386_CR4_VME);
267433d6423SLionel Sambuc 	FLAG(I386_CR4_PVI);
268433d6423SLionel Sambuc 	FLAG(I386_CR4_TSD);
269433d6423SLionel Sambuc 	FLAG(I386_CR4_DE);
270433d6423SLionel Sambuc 	FLAG(I386_CR4_PSE);
271433d6423SLionel Sambuc 	FLAG(I386_CR4_PAE);
272433d6423SLionel Sambuc 	FLAG(I386_CR4_MCE);
273433d6423SLionel Sambuc 	FLAG(I386_CR4_PGE);
274433d6423SLionel Sambuc 	if(e) { strcat(str, " (++)"); }
275433d6423SLionel Sambuc 	return str;
276433d6423SLionel Sambuc }
277433d6423SLionel Sambuc #endif
278433d6423SLionel Sambuc 
279433d6423SLionel Sambuc /*===========================================================================*
280433d6423SLionel Sambuc  *                              umap_virtual                                 *
281433d6423SLionel Sambuc  *===========================================================================*/
umap_virtual(register struct proc * rp,int seg,vir_bytes vir_addr,vir_bytes bytes)282*6077d1adSDr. Florian Grätz phys_bytes umap_virtual(
283*6077d1adSDr. Florian Grätz   register struct proc *rp,		/* pointer to proc table entry for process */
284*6077d1adSDr. Florian Grätz   int seg,				/* T, D, or S segment */
285*6077d1adSDr. Florian Grätz   vir_bytes vir_addr,			/* virtual address in bytes within the seg */
286*6077d1adSDr. Florian Grätz   vir_bytes bytes			/* # of bytes to be copied */
287*6077d1adSDr. Florian Grätz )
288433d6423SLionel Sambuc {
289433d6423SLionel Sambuc 	phys_bytes phys = 0;
290433d6423SLionel Sambuc 
291433d6423SLionel Sambuc 	if(vm_lookup(rp, vir_addr, &phys, NULL) != OK) {
292433d6423SLionel Sambuc 		printf("SYSTEM:umap_virtual: vm_lookup of %s: seg 0x%x: 0x%lx failed\n", rp->p_name, seg, vir_addr);
293433d6423SLionel Sambuc 		phys = 0;
294433d6423SLionel Sambuc 	} else {
295433d6423SLionel Sambuc 		if(phys == 0)
296433d6423SLionel Sambuc 			panic("vm_lookup returned phys: 0x%lx",  phys);
297433d6423SLionel Sambuc 	}
298433d6423SLionel Sambuc 
299433d6423SLionel Sambuc 	if(phys == 0) {
300433d6423SLionel Sambuc 		printf("SYSTEM:umap_virtual: lookup failed\n");
301433d6423SLionel Sambuc 		return 0;
302433d6423SLionel Sambuc 	}
303433d6423SLionel Sambuc 
304433d6423SLionel Sambuc 	/* Now make sure addresses are contiguous in physical memory
305433d6423SLionel Sambuc 	 * so that the umap makes sense.
306433d6423SLionel Sambuc 	 */
307433d6423SLionel Sambuc 	if(bytes > 0 && vm_lookup_range(rp, vir_addr, NULL, bytes) != bytes) {
308433d6423SLionel Sambuc 		printf("umap_virtual: %s: %lu at 0x%lx (vir 0x%lx) not contiguous\n",
309433d6423SLionel Sambuc 			rp->p_name, bytes, vir_addr, vir_addr);
310433d6423SLionel Sambuc 		return 0;
311433d6423SLionel Sambuc 	}
312433d6423SLionel Sambuc 
313433d6423SLionel Sambuc 	/* phys must be larger than 0 (or the caller will think the call
314433d6423SLionel Sambuc 	 * failed), and address must not cross a page boundary.
315433d6423SLionel Sambuc 	 */
316433d6423SLionel Sambuc 	assert(phys);
317433d6423SLionel Sambuc 
318433d6423SLionel Sambuc 	return phys;
319433d6423SLionel Sambuc }
320433d6423SLionel Sambuc 
321433d6423SLionel Sambuc 
322433d6423SLionel Sambuc /*===========================================================================*
323433d6423SLionel Sambuc  *                              vm_lookup                                    *
324433d6423SLionel Sambuc  *===========================================================================*/
vm_lookup(const struct proc * proc,const vir_bytes virtual,phys_bytes * physical,u32_t * ptent)325433d6423SLionel Sambuc int vm_lookup(const struct proc *proc, const vir_bytes virtual,
326433d6423SLionel Sambuc  phys_bytes *physical, u32_t *ptent)
327433d6423SLionel Sambuc {
328433d6423SLionel Sambuc 	u32_t *root, *pt;
329433d6423SLionel Sambuc 	int pde, pte;
330433d6423SLionel Sambuc 	u32_t pde_v, pte_v;
331433d6423SLionel Sambuc 
332433d6423SLionel Sambuc 	assert(proc);
333433d6423SLionel Sambuc 	assert(physical);
334433d6423SLionel Sambuc 	assert(!isemptyp(proc));
335433d6423SLionel Sambuc 	assert(HASPT(proc));
336433d6423SLionel Sambuc 
337433d6423SLionel Sambuc 	/* Retrieve page directory entry. */
338433d6423SLionel Sambuc 	root = (u32_t *) proc->p_seg.p_cr3;
339433d6423SLionel Sambuc 	assert(!((u32_t) root % I386_PAGE_SIZE));
340433d6423SLionel Sambuc 	pde = I386_VM_PDE(virtual);
341433d6423SLionel Sambuc 	assert(pde >= 0 && pde < I386_VM_DIR_ENTRIES);
342433d6423SLionel Sambuc 	pde_v = phys_get32((u32_t) (root + pde));
343433d6423SLionel Sambuc 
344433d6423SLionel Sambuc 	if(!(pde_v & I386_VM_PRESENT)) {
345433d6423SLionel Sambuc 		return EFAULT;
346433d6423SLionel Sambuc 	}
347433d6423SLionel Sambuc 
348433d6423SLionel Sambuc 	/* We don't expect to ever see this. */
349433d6423SLionel Sambuc 	if(pde_v & I386_VM_BIGPAGE) {
350433d6423SLionel Sambuc 		*physical = pde_v & I386_VM_ADDR_MASK_4MB;
351433d6423SLionel Sambuc 		if(ptent) *ptent = pde_v;
352433d6423SLionel Sambuc 		*physical += virtual & I386_VM_OFFSET_MASK_4MB;
353433d6423SLionel Sambuc 	} else {
354433d6423SLionel Sambuc 		/* Retrieve page table entry. */
355433d6423SLionel Sambuc 		pt = (u32_t *) I386_VM_PFA(pde_v);
356433d6423SLionel Sambuc 		assert(!((u32_t) pt % I386_PAGE_SIZE));
357433d6423SLionel Sambuc 		pte = I386_VM_PTE(virtual);
358433d6423SLionel Sambuc 		assert(pte >= 0 && pte < I386_VM_PT_ENTRIES);
359433d6423SLionel Sambuc 		pte_v = phys_get32((u32_t) (pt + pte));
360433d6423SLionel Sambuc 		if(!(pte_v & I386_VM_PRESENT)) {
361433d6423SLionel Sambuc 			return EFAULT;
362433d6423SLionel Sambuc 		}
363433d6423SLionel Sambuc 
364433d6423SLionel Sambuc 		if(ptent) *ptent = pte_v;
365433d6423SLionel Sambuc 
366433d6423SLionel Sambuc 		/* Actual address now known; retrieve it and add page offset. */
367433d6423SLionel Sambuc 		*physical = I386_VM_PFA(pte_v);
368433d6423SLionel Sambuc 		*physical += virtual % I386_PAGE_SIZE;
369433d6423SLionel Sambuc 	}
370433d6423SLionel Sambuc 
371433d6423SLionel Sambuc 	return OK;
372433d6423SLionel Sambuc }
373433d6423SLionel Sambuc 
374433d6423SLionel Sambuc /*===========================================================================*
375433d6423SLionel Sambuc  *				vm_lookup_range				     *
376433d6423SLionel Sambuc  *===========================================================================*/
vm_lookup_range(const struct proc * proc,vir_bytes vir_addr,phys_bytes * phys_addr,size_t bytes)377433d6423SLionel Sambuc size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr,
378433d6423SLionel Sambuc 	phys_bytes *phys_addr, size_t bytes)
379433d6423SLionel Sambuc {
380433d6423SLionel Sambuc 	/* Look up the physical address corresponding to linear virtual address
381433d6423SLionel Sambuc 	 * 'vir_addr' for process 'proc'. Return the size of the range covered
382433d6423SLionel Sambuc 	 * by contiguous physical memory starting from that address; this may
383433d6423SLionel Sambuc 	 * be anywhere between 0 and 'bytes' inclusive. If the return value is
384433d6423SLionel Sambuc 	 * nonzero, and 'phys_addr' is non-NULL, 'phys_addr' will be set to the
385433d6423SLionel Sambuc 	 * base physical address of the range. 'vir_addr' and 'bytes' need not
386433d6423SLionel Sambuc 	 * be page-aligned, but the caller must have verified that the given
387433d6423SLionel Sambuc 	 * linear range is valid for the given process at all.
388433d6423SLionel Sambuc 	 */
389433d6423SLionel Sambuc 	phys_bytes phys, next_phys;
390433d6423SLionel Sambuc 	size_t len;
391433d6423SLionel Sambuc 
392433d6423SLionel Sambuc 	assert(proc);
393433d6423SLionel Sambuc 	assert(bytes > 0);
394433d6423SLionel Sambuc 	assert(HASPT(proc));
395433d6423SLionel Sambuc 
396433d6423SLionel Sambuc 	/* Look up the first page. */
397433d6423SLionel Sambuc 	if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
398433d6423SLionel Sambuc 		return 0;
399433d6423SLionel Sambuc 
400433d6423SLionel Sambuc 	if (phys_addr != NULL)
401433d6423SLionel Sambuc 		*phys_addr = phys;
402433d6423SLionel Sambuc 
403433d6423SLionel Sambuc 	len = I386_PAGE_SIZE - (vir_addr % I386_PAGE_SIZE);
404433d6423SLionel Sambuc 	vir_addr += len;
405433d6423SLionel Sambuc 	next_phys = phys + len;
406433d6423SLionel Sambuc 
407433d6423SLionel Sambuc 	/* Look up any next pages and test physical contiguity. */
408433d6423SLionel Sambuc 	while (len < bytes) {
409433d6423SLionel Sambuc 		if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
410433d6423SLionel Sambuc 			break;
411433d6423SLionel Sambuc 
412433d6423SLionel Sambuc 		if (next_phys != phys)
413433d6423SLionel Sambuc 			break;
414433d6423SLionel Sambuc 
415433d6423SLionel Sambuc 		len += I386_PAGE_SIZE;
416433d6423SLionel Sambuc 		vir_addr += I386_PAGE_SIZE;
417433d6423SLionel Sambuc 		next_phys += I386_PAGE_SIZE;
418433d6423SLionel Sambuc 	}
419433d6423SLionel Sambuc 
420433d6423SLionel Sambuc 	/* We might now have overshot the requested length somewhat. */
421433d6423SLionel Sambuc 	return MIN(bytes, len);
422433d6423SLionel Sambuc }
423433d6423SLionel Sambuc 
424433d6423SLionel Sambuc /*===========================================================================*
425433d6423SLionel Sambuc  *				vm_check_range				     *
426433d6423SLionel Sambuc  *===========================================================================*/
vm_check_range(struct proc * caller,struct proc * target,vir_bytes vir_addr,size_t bytes,int writeflag)427433d6423SLionel Sambuc int vm_check_range(struct proc *caller, struct proc *target,
428433d6423SLionel Sambuc 	vir_bytes vir_addr, size_t bytes, int writeflag)
429433d6423SLionel Sambuc {
430433d6423SLionel Sambuc 	/* Public interface to vm_suspend(), for use by kernel calls. On behalf
431433d6423SLionel Sambuc 	 * of 'caller', call into VM to check linear virtual address range of
432433d6423SLionel Sambuc 	 * process 'target', starting at 'vir_addr', for 'bytes' bytes. This
433433d6423SLionel Sambuc 	 * function assumes that it will called twice if VM returned an error
434433d6423SLionel Sambuc 	 * the first time (since nothing has changed in that case), and will
435433d6423SLionel Sambuc 	 * then return the error code resulting from the first call. Upon the
436433d6423SLionel Sambuc 	 * first call, a non-success error code is returned as well.
437433d6423SLionel Sambuc 	 */
438433d6423SLionel Sambuc 	int r;
439433d6423SLionel Sambuc 
440433d6423SLionel Sambuc 	if ((caller->p_misc_flags & MF_KCALL_RESUME) &&
441433d6423SLionel Sambuc 			(r = caller->p_vmrequest.vmresult) != OK)
442433d6423SLionel Sambuc 		return r;
443433d6423SLionel Sambuc 
444433d6423SLionel Sambuc 	vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL,
445433d6423SLionel Sambuc 		writeflag);
446433d6423SLionel Sambuc 
447433d6423SLionel Sambuc 	return VMSUSPEND;
448433d6423SLionel Sambuc }
449433d6423SLionel Sambuc 
450433d6423SLionel Sambuc #if 0
451433d6423SLionel Sambuc static char *flagstr(u32_t e, const int dir)
452433d6423SLionel Sambuc {
453433d6423SLionel Sambuc 	static char str[80];
454433d6423SLionel Sambuc 	strcpy(str, "");
455433d6423SLionel Sambuc 	FLAG(I386_VM_PRESENT);
456433d6423SLionel Sambuc 	FLAG(I386_VM_WRITE);
457433d6423SLionel Sambuc 	FLAG(I386_VM_USER);
458433d6423SLionel Sambuc 	FLAG(I386_VM_PWT);
459433d6423SLionel Sambuc 	FLAG(I386_VM_PCD);
460433d6423SLionel Sambuc 	FLAG(I386_VM_GLOBAL);
461433d6423SLionel Sambuc 	if(dir)
462433d6423SLionel Sambuc 		FLAG(I386_VM_BIGPAGE);	/* Page directory entry only */
463433d6423SLionel Sambuc 	else
464433d6423SLionel Sambuc 		FLAG(I386_VM_DIRTY);	/* Page table entry only */
465433d6423SLionel Sambuc 	return str;
466433d6423SLionel Sambuc }
467433d6423SLionel Sambuc 
468433d6423SLionel Sambuc static void vm_pt_print(u32_t *pagetable, const u32_t v)
469433d6423SLionel Sambuc {
470433d6423SLionel Sambuc 	int pte;
471433d6423SLionel Sambuc 	int col = 0;
472433d6423SLionel Sambuc 
473433d6423SLionel Sambuc 	assert(!((u32_t) pagetable % I386_PAGE_SIZE));
474433d6423SLionel Sambuc 
475433d6423SLionel Sambuc 	for(pte = 0; pte < I386_VM_PT_ENTRIES; pte++) {
476433d6423SLionel Sambuc 		u32_t pte_v, pfa;
477433d6423SLionel Sambuc 		pte_v = phys_get32((u32_t) (pagetable + pte));
478433d6423SLionel Sambuc 		if(!(pte_v & I386_VM_PRESENT))
479433d6423SLionel Sambuc 			continue;
480433d6423SLionel Sambuc 		pfa = I386_VM_PFA(pte_v);
481433d6423SLionel Sambuc 		printf("%4d:%08lx:%08lx %2s ",
482433d6423SLionel Sambuc 			pte, v + I386_PAGE_SIZE*pte, pfa,
483433d6423SLionel Sambuc 			(pte_v & I386_VM_WRITE) ? "rw":"RO");
484433d6423SLionel Sambuc 		col++;
485433d6423SLionel Sambuc 		if(col == 3) { printf("\n"); col = 0; }
486433d6423SLionel Sambuc 	}
487433d6423SLionel Sambuc 	if(col > 0) printf("\n");
488433d6423SLionel Sambuc 
489433d6423SLionel Sambuc 	return;
490433d6423SLionel Sambuc }
491433d6423SLionel Sambuc 
492433d6423SLionel Sambuc static void vm_print(u32_t *root)
493433d6423SLionel Sambuc {
494433d6423SLionel Sambuc 	int pde;
495433d6423SLionel Sambuc 
496433d6423SLionel Sambuc 	assert(!((u32_t) root % I386_PAGE_SIZE));
497433d6423SLionel Sambuc 
498433d6423SLionel Sambuc 	printf("page table 0x%lx:\n", root);
499433d6423SLionel Sambuc 
500433d6423SLionel Sambuc 	for(pde = 0; pde < I386_VM_DIR_ENTRIES; pde++) {
501433d6423SLionel Sambuc 		u32_t pde_v;
502433d6423SLionel Sambuc 		u32_t *pte_a;
503433d6423SLionel Sambuc 		pde_v = phys_get32((u32_t) (root + pde));
504433d6423SLionel Sambuc 		if(!(pde_v & I386_VM_PRESENT))
505433d6423SLionel Sambuc 			continue;
506433d6423SLionel Sambuc 		if(pde_v & I386_VM_BIGPAGE) {
507433d6423SLionel Sambuc 			printf("%4d: 0x%lx, flags %s\n",
508433d6423SLionel Sambuc 				pde, I386_VM_PFA(pde_v), flagstr(pde_v, 1));
509433d6423SLionel Sambuc 		} else {
510433d6423SLionel Sambuc 			pte_a = (u32_t *) I386_VM_PFA(pde_v);
511433d6423SLionel Sambuc 			printf("%4d: pt %08lx %s\n",
512433d6423SLionel Sambuc 				pde, pte_a, flagstr(pde_v, 1));
513433d6423SLionel Sambuc 			vm_pt_print(pte_a, pde * I386_VM_PT_ENTRIES * I386_PAGE_SIZE);
514433d6423SLionel Sambuc 			printf("\n");
515433d6423SLionel Sambuc 		}
516433d6423SLionel Sambuc 	}
517433d6423SLionel Sambuc 
518433d6423SLionel Sambuc 
519433d6423SLionel Sambuc 	return;
520433d6423SLionel Sambuc }
521433d6423SLionel Sambuc #endif
522433d6423SLionel Sambuc 
523433d6423SLionel Sambuc /*===========================================================================*
524433d6423SLionel Sambuc  *                                 vmmemset                                  *
525433d6423SLionel Sambuc  *===========================================================================*/
vm_memset(struct proc * caller,endpoint_t who,phys_bytes ph,int c,phys_bytes count)526433d6423SLionel Sambuc int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c,
527433d6423SLionel Sambuc 	phys_bytes count)
528433d6423SLionel Sambuc {
529433d6423SLionel Sambuc 	u32_t pattern;
530433d6423SLionel Sambuc 	struct proc *whoptr = NULL;
531433d6423SLionel Sambuc 	phys_bytes cur_ph = ph;
532433d6423SLionel Sambuc 	phys_bytes left = count;
533433d6423SLionel Sambuc 	phys_bytes ptr, chunk, pfa = 0;
534433d6423SLionel Sambuc 	int new_cr3, r = OK;
535433d6423SLionel Sambuc 
536433d6423SLionel Sambuc 	if ((r = check_resumed_caller(caller)) != OK)
537433d6423SLionel Sambuc 		return r;
538433d6423SLionel Sambuc 
539433d6423SLionel Sambuc 	/* NONE for physical, otherwise virtual */
540433d6423SLionel Sambuc 	if (who != NONE && !(whoptr = endpoint_lookup(who)))
541433d6423SLionel Sambuc 		return ESRCH;
542433d6423SLionel Sambuc 
543433d6423SLionel Sambuc 	c &= 0xFF;
544433d6423SLionel Sambuc 	pattern = c | (c << 8) | (c << 16) | (c << 24);
545433d6423SLionel Sambuc 
546433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
547433d6423SLionel Sambuc 	assert(!catch_pagefaults);
548433d6423SLionel Sambuc 	catch_pagefaults = 1;
549433d6423SLionel Sambuc 
550433d6423SLionel Sambuc 	/* We can memset as many bytes as we have remaining,
551433d6423SLionel Sambuc 	 * or as many as remain in the 4MB chunk we mapped in.
552433d6423SLionel Sambuc 	 */
553433d6423SLionel Sambuc 	while (left > 0) {
554433d6423SLionel Sambuc 		new_cr3 = 0;
555433d6423SLionel Sambuc 		chunk = left;
556433d6423SLionel Sambuc 		ptr = createpde(whoptr, cur_ph, &chunk, 0, &new_cr3);
557433d6423SLionel Sambuc 
558433d6423SLionel Sambuc 		if (new_cr3)
559433d6423SLionel Sambuc 			reload_cr3();
560433d6423SLionel Sambuc 
561433d6423SLionel Sambuc 		/* If a page fault happens, pfa is non-null */
562433d6423SLionel Sambuc 		if ((pfa = phys_memset(ptr, pattern, chunk))) {
563433d6423SLionel Sambuc 
564433d6423SLionel Sambuc 			/* If a process pagefaults, VM may help out */
565433d6423SLionel Sambuc 			if (whoptr) {
566433d6423SLionel Sambuc 				vm_suspend(caller, whoptr, ph, count,
567433d6423SLionel Sambuc 						   VMSTYPE_KERNELCALL, 1);
568433d6423SLionel Sambuc 				assert(catch_pagefaults);
569433d6423SLionel Sambuc 				catch_pagefaults = 0;
570433d6423SLionel Sambuc 				return VMSUSPEND;
571433d6423SLionel Sambuc 			}
572433d6423SLionel Sambuc 
573433d6423SLionel Sambuc 			/* Pagefault when phys copying ?! */
574433d6423SLionel Sambuc 			panic("vm_memset: pf %lx addr=%lx len=%lu\n",
575433d6423SLionel Sambuc 						pfa , ptr, chunk);
576433d6423SLionel Sambuc 		}
577433d6423SLionel Sambuc 
578433d6423SLionel Sambuc 		cur_ph += chunk;
579433d6423SLionel Sambuc 		left -= chunk;
580433d6423SLionel Sambuc 	}
581433d6423SLionel Sambuc 
582433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v);
583433d6423SLionel Sambuc 	assert(catch_pagefaults);
584433d6423SLionel Sambuc 	catch_pagefaults = 0;
585433d6423SLionel Sambuc 
586433d6423SLionel Sambuc 	return OK;
587433d6423SLionel Sambuc }
588433d6423SLionel Sambuc 
589433d6423SLionel Sambuc /*===========================================================================*
590433d6423SLionel Sambuc  *				virtual_copy_f				     *
591433d6423SLionel Sambuc  *===========================================================================*/
virtual_copy_f(struct proc * caller,struct vir_addr * src_addr,struct vir_addr * dst_addr,vir_bytes bytes,int vmcheck)592*6077d1adSDr. Florian Grätz int virtual_copy_f(
593*6077d1adSDr. Florian Grätz   struct proc * caller,
594*6077d1adSDr. Florian Grätz   struct vir_addr *src_addr,	/* source virtual address */
595*6077d1adSDr. Florian Grätz   struct vir_addr *dst_addr,	/* destination virtual address */
596*6077d1adSDr. Florian Grätz   vir_bytes bytes,		/* # of bytes to copy  */
597*6077d1adSDr. Florian Grätz   int vmcheck			/* if nonzero, can return VMSUSPEND */
598*6077d1adSDr. Florian Grätz )
599433d6423SLionel Sambuc {
600433d6423SLionel Sambuc /* Copy bytes from virtual address src_addr to virtual address dst_addr. */
601433d6423SLionel Sambuc   struct vir_addr *vir_addr[2];	/* virtual source and destination address */
602433d6423SLionel Sambuc   int i, r;
603433d6423SLionel Sambuc   struct proc *procs[2];
604433d6423SLionel Sambuc 
605433d6423SLionel Sambuc   assert((vmcheck && caller) || (!vmcheck && !caller));
606433d6423SLionel Sambuc 
607433d6423SLionel Sambuc   /* Check copy count. */
608433d6423SLionel Sambuc   if (bytes <= 0) return(EDOM);
609433d6423SLionel Sambuc 
610433d6423SLionel Sambuc   /* Do some more checks and map virtual addresses to physical addresses. */
611433d6423SLionel Sambuc   vir_addr[_SRC_] = src_addr;
612433d6423SLionel Sambuc   vir_addr[_DST_] = dst_addr;
613433d6423SLionel Sambuc 
614433d6423SLionel Sambuc   for (i=_SRC_; i<=_DST_; i++) {
615433d6423SLionel Sambuc   	endpoint_t proc_e = vir_addr[i]->proc_nr_e;
616433d6423SLionel Sambuc 	int proc_nr;
617433d6423SLionel Sambuc 	struct proc *p;
618433d6423SLionel Sambuc 
619433d6423SLionel Sambuc 	if(proc_e == NONE) {
620433d6423SLionel Sambuc 		p = NULL;
621433d6423SLionel Sambuc 	} else {
622433d6423SLionel Sambuc 		if(!isokendpt(proc_e, &proc_nr)) {
623433d6423SLionel Sambuc 			printf("virtual_copy: no reasonable endpoint\n");
624433d6423SLionel Sambuc 			return ESRCH;
625433d6423SLionel Sambuc 		}
626433d6423SLionel Sambuc 		p = proc_addr(proc_nr);
627433d6423SLionel Sambuc 	}
628433d6423SLionel Sambuc 
629433d6423SLionel Sambuc 	procs[i] = p;
630433d6423SLionel Sambuc   }
631433d6423SLionel Sambuc 
632433d6423SLionel Sambuc   if ((r = check_resumed_caller(caller)) != OK)
633433d6423SLionel Sambuc 	return r;
634433d6423SLionel Sambuc 
635433d6423SLionel Sambuc   if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset,
636433d6423SLionel Sambuc   	procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) {
637433d6423SLionel Sambuc 	int writeflag;
638433d6423SLionel Sambuc   	struct proc *target = NULL;
639433d6423SLionel Sambuc   	phys_bytes lin;
640433d6423SLionel Sambuc   	if(r != EFAULT_SRC && r != EFAULT_DST)
641433d6423SLionel Sambuc   		panic("lin_lin_copy failed: %d",  r);
642433d6423SLionel Sambuc   	if(!vmcheck || !caller) {
643433d6423SLionel Sambuc     		return r;
644433d6423SLionel Sambuc   	}
645433d6423SLionel Sambuc 
646433d6423SLionel Sambuc   	if(r == EFAULT_SRC) {
647433d6423SLionel Sambuc   		lin = vir_addr[_SRC_]->offset;
648433d6423SLionel Sambuc   		target = procs[_SRC_];
649433d6423SLionel Sambuc 		writeflag = 0;
650433d6423SLionel Sambuc   	} else if(r == EFAULT_DST) {
651433d6423SLionel Sambuc   		lin = vir_addr[_DST_]->offset;
652433d6423SLionel Sambuc   		target = procs[_DST_];
653433d6423SLionel Sambuc 		writeflag = 1;
654433d6423SLionel Sambuc   	} else {
655433d6423SLionel Sambuc   		panic("r strange: %d",  r);
656433d6423SLionel Sambuc   	}
657433d6423SLionel Sambuc 
658433d6423SLionel Sambuc 	assert(caller);
659433d6423SLionel Sambuc 	assert(target);
660433d6423SLionel Sambuc 
661433d6423SLionel Sambuc 	vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag);
662433d6423SLionel Sambuc 	return VMSUSPEND;
663433d6423SLionel Sambuc   }
664433d6423SLionel Sambuc 
665433d6423SLionel Sambuc   return OK;
666433d6423SLionel Sambuc }
667433d6423SLionel Sambuc 
668433d6423SLionel Sambuc /*===========================================================================*
669433d6423SLionel Sambuc  *				data_copy				     *
670433d6423SLionel Sambuc  *===========================================================================*/
data_copy(const endpoint_t from_proc,const vir_bytes from_addr,const endpoint_t to_proc,const vir_bytes to_addr,size_t bytes)671433d6423SLionel Sambuc int data_copy(const endpoint_t from_proc, const vir_bytes from_addr,
672433d6423SLionel Sambuc 	const endpoint_t to_proc, const vir_bytes to_addr,
673433d6423SLionel Sambuc 	size_t bytes)
674433d6423SLionel Sambuc {
675433d6423SLionel Sambuc   struct vir_addr src, dst;
676433d6423SLionel Sambuc 
677433d6423SLionel Sambuc   src.offset = from_addr;
678433d6423SLionel Sambuc   dst.offset = to_addr;
679433d6423SLionel Sambuc   src.proc_nr_e = from_proc;
680433d6423SLionel Sambuc   dst.proc_nr_e = to_proc;
681433d6423SLionel Sambuc   assert(src.proc_nr_e != NONE);
682433d6423SLionel Sambuc   assert(dst.proc_nr_e != NONE);
683433d6423SLionel Sambuc 
684433d6423SLionel Sambuc   return virtual_copy(&src, &dst, bytes);
685433d6423SLionel Sambuc }
686433d6423SLionel Sambuc 
687433d6423SLionel Sambuc /*===========================================================================*
688433d6423SLionel Sambuc  *				data_copy_vmcheck			     *
689433d6423SLionel Sambuc  *===========================================================================*/
data_copy_vmcheck(struct proc * caller,const endpoint_t from_proc,const vir_bytes from_addr,const endpoint_t to_proc,const vir_bytes to_addr,size_t bytes)690433d6423SLionel Sambuc int data_copy_vmcheck(struct proc * caller,
691433d6423SLionel Sambuc 	const endpoint_t from_proc, const vir_bytes from_addr,
692433d6423SLionel Sambuc 	const endpoint_t to_proc, const vir_bytes to_addr,
693433d6423SLionel Sambuc 	size_t bytes)
694433d6423SLionel Sambuc {
695433d6423SLionel Sambuc   struct vir_addr src, dst;
696433d6423SLionel Sambuc 
697433d6423SLionel Sambuc   src.offset = from_addr;
698433d6423SLionel Sambuc   dst.offset = to_addr;
699433d6423SLionel Sambuc   src.proc_nr_e = from_proc;
700433d6423SLionel Sambuc   dst.proc_nr_e = to_proc;
701433d6423SLionel Sambuc   assert(src.proc_nr_e != NONE);
702433d6423SLionel Sambuc   assert(dst.proc_nr_e != NONE);
703433d6423SLionel Sambuc 
704433d6423SLionel Sambuc   return virtual_copy_vmcheck(caller, &src, &dst, bytes);
705433d6423SLionel Sambuc }
706433d6423SLionel Sambuc 
memory_init(void)707433d6423SLionel Sambuc void memory_init(void)
708433d6423SLionel Sambuc {
709433d6423SLionel Sambuc 	assert(nfreepdes == 0);
710433d6423SLionel Sambuc 
711433d6423SLionel Sambuc 	freepdes[nfreepdes++] = kinfo.freepde_start++;
712433d6423SLionel Sambuc 	freepdes[nfreepdes++] = kinfo.freepde_start++;
713433d6423SLionel Sambuc 
714433d6423SLionel Sambuc 	assert(kinfo.freepde_start < I386_VM_DIR_ENTRIES);
715433d6423SLionel Sambuc 	assert(nfreepdes == 2);
716433d6423SLionel Sambuc 	assert(nfreepdes <= MAXFREEPDES);
717433d6423SLionel Sambuc }
718433d6423SLionel Sambuc 
719433d6423SLionel Sambuc /*===========================================================================*
720433d6423SLionel Sambuc  *				arch_proc_init				     *
721433d6423SLionel Sambuc  *===========================================================================*/
arch_proc_init(struct proc * pr,const u32_t ip,const u32_t sp,const u32_t ps_str,char * name)722433d6423SLionel Sambuc void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp,
723433d6423SLionel Sambuc 	const u32_t ps_str, char *name)
724433d6423SLionel Sambuc {
725433d6423SLionel Sambuc 	arch_proc_reset(pr);
726433d6423SLionel Sambuc 	strlcpy(pr->p_name, name, sizeof(pr->p_name));
727433d6423SLionel Sambuc 
728433d6423SLionel Sambuc 	/* set custom state we know */
729433d6423SLionel Sambuc 	pr->p_reg.pc = ip;
730433d6423SLionel Sambuc 	pr->p_reg.sp = sp;
731433d6423SLionel Sambuc 	pr->p_reg.bx = ps_str;
732433d6423SLionel Sambuc }
733433d6423SLionel Sambuc 
734433d6423SLionel Sambuc static int oxpcie_mapping_index = -1,
735433d6423SLionel Sambuc 	lapic_mapping_index = -1,
736433d6423SLionel Sambuc 	ioapic_first_index = -1,
737433d6423SLionel Sambuc 	ioapic_last_index = -1,
738433d6423SLionel Sambuc 	video_mem_mapping_index = -1,
739433d6423SLionel Sambuc 	usermapped_glo_index = -1,
740433d6423SLionel Sambuc 	usermapped_index = -1, first_um_idx = -1;
741433d6423SLionel Sambuc 
742433d6423SLionel Sambuc extern char *video_mem;
743433d6423SLionel Sambuc 
744433d6423SLionel Sambuc extern char usermapped_start, usermapped_end, usermapped_nonglo_start;
745433d6423SLionel Sambuc 
arch_phys_map(const int index,phys_bytes * addr,phys_bytes * len,int * flags)746433d6423SLionel Sambuc int arch_phys_map(const int index,
747433d6423SLionel Sambuc 			phys_bytes *addr,
748433d6423SLionel Sambuc 			phys_bytes *len,
749433d6423SLionel Sambuc 			int *flags)
750433d6423SLionel Sambuc {
751433d6423SLionel Sambuc 	static int first = 1;
752433d6423SLionel Sambuc 	int freeidx = 0;
753433d6423SLionel Sambuc 	static char *ser_var = NULL;
754433d6423SLionel Sambuc 	u32_t glo_len = (u32_t) &usermapped_nonglo_start -
755433d6423SLionel Sambuc 			(u32_t) &usermapped_start;
756433d6423SLionel Sambuc 
757433d6423SLionel Sambuc 	if(first) {
758433d6423SLionel Sambuc 		memset(&minix_kerninfo, 0, sizeof(minix_kerninfo));
759433d6423SLionel Sambuc 		video_mem_mapping_index = freeidx++;
760433d6423SLionel Sambuc 		if(glo_len > 0) {
761433d6423SLionel Sambuc 			usermapped_glo_index = freeidx++;
762433d6423SLionel Sambuc 		}
763433d6423SLionel Sambuc 
764433d6423SLionel Sambuc 		usermapped_index = freeidx++;
765433d6423SLionel Sambuc 		first_um_idx = usermapped_index;
766433d6423SLionel Sambuc 		if(usermapped_glo_index != -1)
767433d6423SLionel Sambuc 			first_um_idx = usermapped_glo_index;
768433d6423SLionel Sambuc 
769433d6423SLionel Sambuc #ifdef USE_APIC
770433d6423SLionel Sambuc 		if(lapic_addr)
771433d6423SLionel Sambuc 			lapic_mapping_index = freeidx++;
772433d6423SLionel Sambuc 		if (ioapic_enabled) {
773433d6423SLionel Sambuc 			ioapic_first_index = freeidx;
774433d6423SLionel Sambuc 			assert(nioapics > 0);
775433d6423SLionel Sambuc 			freeidx += nioapics;
776433d6423SLionel Sambuc 			ioapic_last_index = freeidx-1;
777433d6423SLionel Sambuc 		}
778433d6423SLionel Sambuc #endif
779433d6423SLionel Sambuc 
780433d6423SLionel Sambuc #ifdef CONFIG_OXPCIE
781433d6423SLionel Sambuc 		if((ser_var = env_get("oxpcie"))) {
782433d6423SLionel Sambuc 			if(ser_var[0] != '0' || ser_var[1] != 'x') {
783433d6423SLionel Sambuc 				printf("oxpcie address in hex please\n");
784433d6423SLionel Sambuc 			} else {
785433d6423SLionel Sambuc 				printf("oxpcie address is %s\n", ser_var);
786433d6423SLionel Sambuc 				oxpcie_mapping_index = freeidx++;
787433d6423SLionel Sambuc 			}
788433d6423SLionel Sambuc 		}
789433d6423SLionel Sambuc #endif
790433d6423SLionel Sambuc 
791433d6423SLionel Sambuc 		first = 0;
792433d6423SLionel Sambuc 	}
793433d6423SLionel Sambuc 
794433d6423SLionel Sambuc 	if(index == usermapped_glo_index) {
795433d6423SLionel Sambuc 		*addr = vir2phys(&usermapped_start);
796433d6423SLionel Sambuc 		*len = glo_len;
797433d6423SLionel Sambuc 		*flags = VMMF_USER | VMMF_GLO;
798433d6423SLionel Sambuc 		return OK;
799433d6423SLionel Sambuc 	}
800433d6423SLionel Sambuc 	else if(index == usermapped_index) {
801433d6423SLionel Sambuc 		*addr = vir2phys(&usermapped_nonglo_start);
802433d6423SLionel Sambuc 		*len = (u32_t) &usermapped_end -
803433d6423SLionel Sambuc 			(u32_t) &usermapped_nonglo_start;
804433d6423SLionel Sambuc 		*flags = VMMF_USER;
805433d6423SLionel Sambuc 		return OK;
806433d6423SLionel Sambuc 	}
807433d6423SLionel Sambuc 	else if (index == video_mem_mapping_index) {
808433d6423SLionel Sambuc 		/* map video memory in so we can print panic messages */
809433d6423SLionel Sambuc 		*addr = MULTIBOOT_VIDEO_BUFFER;
810433d6423SLionel Sambuc 		*len = I386_PAGE_SIZE;
811433d6423SLionel Sambuc 		*flags = VMMF_WRITE;
812433d6423SLionel Sambuc 		return OK;
813433d6423SLionel Sambuc 	}
814433d6423SLionel Sambuc #ifdef USE_APIC
815433d6423SLionel Sambuc 	else if (index == lapic_mapping_index) {
816433d6423SLionel Sambuc 		/* map the local APIC if enabled */
817433d6423SLionel Sambuc 		if (!lapic_addr)
818433d6423SLionel Sambuc 			return EINVAL;
819433d6423SLionel Sambuc 		*addr = lapic_addr;
820433d6423SLionel Sambuc 		*len = 4 << 10 /* 4kB */;
821433d6423SLionel Sambuc 		*flags = VMMF_UNCACHED | VMMF_WRITE;
822433d6423SLionel Sambuc 		return OK;
823433d6423SLionel Sambuc 	}
824433d6423SLionel Sambuc 	else if (ioapic_enabled && index >= ioapic_first_index && index <= ioapic_last_index) {
825433d6423SLionel Sambuc 		int ioapic_idx = index - ioapic_first_index;
826433d6423SLionel Sambuc 		*addr = io_apic[ioapic_idx].paddr;
827433d6423SLionel Sambuc 		assert(*addr);
828433d6423SLionel Sambuc 		*len = 4 << 10 /* 4kB */;
829433d6423SLionel Sambuc 		*flags = VMMF_UNCACHED | VMMF_WRITE;
830433d6423SLionel Sambuc 		printf("ioapic map: addr 0x%lx\n", *addr);
831433d6423SLionel Sambuc 		return OK;
832433d6423SLionel Sambuc 	}
833433d6423SLionel Sambuc #endif
834433d6423SLionel Sambuc 
835433d6423SLionel Sambuc #if CONFIG_OXPCIE
836433d6423SLionel Sambuc 	if(index == oxpcie_mapping_index) {
837433d6423SLionel Sambuc 		*addr = strtoul(ser_var+2, NULL, 16);
838433d6423SLionel Sambuc 		*len = 0x4000;
839433d6423SLionel Sambuc 		*flags = VMMF_UNCACHED | VMMF_WRITE;
840433d6423SLionel Sambuc 		return OK;
841433d6423SLionel Sambuc 	}
842433d6423SLionel Sambuc #endif
843433d6423SLionel Sambuc 
844433d6423SLionel Sambuc 	return EINVAL;
845433d6423SLionel Sambuc }
846433d6423SLionel Sambuc 
arch_phys_map_reply(const int index,const vir_bytes addr)847433d6423SLionel Sambuc int arch_phys_map_reply(const int index, const vir_bytes addr)
848433d6423SLionel Sambuc {
849433d6423SLionel Sambuc #ifdef USE_APIC
850433d6423SLionel Sambuc 	/* if local APIC is enabled */
851433d6423SLionel Sambuc 	if (index == lapic_mapping_index && lapic_addr) {
852433d6423SLionel Sambuc 		lapic_addr_vaddr = addr;
853433d6423SLionel Sambuc 		return OK;
854433d6423SLionel Sambuc 	}
855433d6423SLionel Sambuc 	else if (ioapic_enabled && index >= ioapic_first_index &&
856433d6423SLionel Sambuc 		index <= ioapic_last_index) {
857433d6423SLionel Sambuc 		int i = index - ioapic_first_index;
858433d6423SLionel Sambuc 		io_apic[i].vaddr = addr;
859433d6423SLionel Sambuc 		return OK;
860433d6423SLionel Sambuc 	}
861433d6423SLionel Sambuc #endif
862433d6423SLionel Sambuc 
863433d6423SLionel Sambuc #if CONFIG_OXPCIE
864433d6423SLionel Sambuc 	if (index == oxpcie_mapping_index) {
865433d6423SLionel Sambuc 		oxpcie_set_vaddr((unsigned char *) addr);
866433d6423SLionel Sambuc 		return OK;
867433d6423SLionel Sambuc 	}
868433d6423SLionel Sambuc #endif
869433d6423SLionel Sambuc 	if(index == first_um_idx) {
870433d6423SLionel Sambuc 		extern struct minix_ipcvecs minix_ipcvecs_sysenter,
871433d6423SLionel Sambuc 			minix_ipcvecs_syscall,
872433d6423SLionel Sambuc 			minix_ipcvecs_softint;
873433d6423SLionel Sambuc 		extern u32_t usermapped_offset;
874433d6423SLionel Sambuc 		assert(addr > (u32_t) &usermapped_start);
875433d6423SLionel Sambuc 		usermapped_offset = addr - (u32_t) &usermapped_start;
876433d6423SLionel Sambuc #define FIXEDPTR(ptr) (void *) ((u32_t)ptr + usermapped_offset)
877433d6423SLionel Sambuc #define FIXPTR(ptr) ptr = FIXEDPTR(ptr)
878433d6423SLionel Sambuc #define ASSIGN(minixstruct) minix_kerninfo.minixstruct = FIXEDPTR(&minixstruct)
879433d6423SLionel Sambuc 		ASSIGN(kinfo);
880433d6423SLionel Sambuc 		ASSIGN(machine);
881433d6423SLionel Sambuc 		ASSIGN(kmessages);
882433d6423SLionel Sambuc 		ASSIGN(loadinfo);
88320054ae9SDavid van Moolenbroek 		ASSIGN(kuserinfo);
88426f5c8f8SDavid van Moolenbroek 		ASSIGN(arm_frclock); /* eh, why not. */
885d91f738bSDavid van Moolenbroek 		ASSIGN(kclockinfo);
886433d6423SLionel Sambuc 
887433d6423SLionel Sambuc 		/* select the right set of IPC routines to map into processes */
888433d6423SLionel Sambuc 		if(minix_feature_flags & MKF_I386_INTEL_SYSENTER) {
889da9af514SLionel Sambuc 			DEBUGBASIC(("kernel: selecting intel sysenter ipc style\n"));
890433d6423SLionel Sambuc 			minix_kerninfo.minix_ipcvecs = &minix_ipcvecs_sysenter;
891433d6423SLionel Sambuc 		} else  if(minix_feature_flags & MKF_I386_AMD_SYSCALL) {
892da9af514SLionel Sambuc 			DEBUGBASIC(("kernel: selecting amd syscall ipc style\n"));
893433d6423SLionel Sambuc 			minix_kerninfo.minix_ipcvecs = &minix_ipcvecs_syscall;
894433d6423SLionel Sambuc 		} else	{
895da9af514SLionel Sambuc 			DEBUGBASIC(("kernel: selecting fallback (int) ipc style\n"));
896433d6423SLionel Sambuc 			minix_kerninfo.minix_ipcvecs = &minix_ipcvecs_softint;
897433d6423SLionel Sambuc 		}
898433d6423SLionel Sambuc 
899433d6423SLionel Sambuc 		/* adjust the pointers of the functions and the struct
900433d6423SLionel Sambuc 		 * itself to the user-accessible mapping
901433d6423SLionel Sambuc 		 */
902433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs->send);
903433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs->receive);
904433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs->sendrec);
905433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs->senda);
906433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs->sendnb);
907433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs->notify);
908433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs->do_kernel_call);
909433d6423SLionel Sambuc 		FIXPTR(minix_kerninfo.minix_ipcvecs);
910433d6423SLionel Sambuc 
911433d6423SLionel Sambuc 		minix_kerninfo.kerninfo_magic = KERNINFO_MAGIC;
912433d6423SLionel Sambuc 		minix_kerninfo.minix_feature_flags = minix_feature_flags;
913433d6423SLionel Sambuc 		minix_kerninfo_user = (vir_bytes) FIXEDPTR(&minix_kerninfo);
914433d6423SLionel Sambuc 
915433d6423SLionel Sambuc 		/* if libc_ipc is set, disable usermapped ipc functions
916433d6423SLionel Sambuc 		 * and force binaries to use in-libc fallbacks.
917433d6423SLionel Sambuc 		 */
918433d6423SLionel Sambuc 		if(env_get("libc_ipc")) {
919433d6423SLionel Sambuc 			printf("kernel: forcing in-libc fallback ipc style\n");
920433d6423SLionel Sambuc 			minix_kerninfo.minix_ipcvecs = NULL;
921433d6423SLionel Sambuc 		} else {
922433d6423SLionel Sambuc 			minix_kerninfo.ki_flags |= MINIX_KIF_IPCVECS;
923433d6423SLionel Sambuc 		}
924433d6423SLionel Sambuc 
92520054ae9SDavid van Moolenbroek 		minix_kerninfo.ki_flags |= MINIX_KIF_USERINFO;
92620054ae9SDavid van Moolenbroek 
927433d6423SLionel Sambuc 		return OK;
928433d6423SLionel Sambuc 	}
929433d6423SLionel Sambuc 
930433d6423SLionel Sambuc 	if(index == usermapped_index) return OK;
931433d6423SLionel Sambuc 
932433d6423SLionel Sambuc 	if (index == video_mem_mapping_index) {
933433d6423SLionel Sambuc 		video_mem_vaddr =  addr;
934433d6423SLionel Sambuc 		return OK;
935433d6423SLionel Sambuc 	}
936433d6423SLionel Sambuc 
937433d6423SLionel Sambuc 	return EINVAL;
938433d6423SLionel Sambuc }
939433d6423SLionel Sambuc 
arch_enable_paging(struct proc * caller)940433d6423SLionel Sambuc int arch_enable_paging(struct proc * caller)
941433d6423SLionel Sambuc {
942433d6423SLionel Sambuc 	assert(caller->p_seg.p_cr3);
943433d6423SLionel Sambuc 
944433d6423SLionel Sambuc 	/* load caller's page table */
945433d6423SLionel Sambuc 	switch_address_space(caller);
946433d6423SLionel Sambuc 
947433d6423SLionel Sambuc 	video_mem = (char *) video_mem_vaddr;
948433d6423SLionel Sambuc 
949433d6423SLionel Sambuc #ifdef USE_APIC
950433d6423SLionel Sambuc 	/* start using the virtual addresses */
951433d6423SLionel Sambuc 
952433d6423SLionel Sambuc 	/* if local APIC is enabled */
953433d6423SLionel Sambuc 	if (lapic_addr) {
954433d6423SLionel Sambuc 		lapic_addr = lapic_addr_vaddr;
955433d6423SLionel Sambuc 		lapic_eoi_addr = LAPIC_EOI;
956433d6423SLionel Sambuc 	}
957433d6423SLionel Sambuc 	/* if IO apics are enabled */
958433d6423SLionel Sambuc 	if (ioapic_enabled) {
959433d6423SLionel Sambuc 		int i;
960433d6423SLionel Sambuc 
961433d6423SLionel Sambuc 		for (i = 0; i < nioapics; i++) {
962433d6423SLionel Sambuc 			io_apic[i].addr = io_apic[i].vaddr;
963433d6423SLionel Sambuc 		}
964433d6423SLionel Sambuc 	}
965433d6423SLionel Sambuc #if CONFIG_SMP
966433d6423SLionel Sambuc 	barrier();
967433d6423SLionel Sambuc 
968433d6423SLionel Sambuc 	wait_for_APs_to_finish_booting();
969433d6423SLionel Sambuc #endif
970433d6423SLionel Sambuc #endif
971433d6423SLionel Sambuc 
972433d6423SLionel Sambuc #ifdef USE_WATCHDOG
973433d6423SLionel Sambuc 	/*
974433d6423SLionel Sambuc 	 * We make sure that we don't enable the watchdog until paging is turned
975433d6423SLionel Sambuc 	 * on as we might get an NMI while switching and we might still use wrong
976433d6423SLionel Sambuc 	 * lapic address. Bad things would happen. It is unfortunate but such is
977433d6423SLionel Sambuc 	 * life
978433d6423SLionel Sambuc 	 */
979433d6423SLionel Sambuc 	if (watchdog_enabled)
980433d6423SLionel Sambuc 		i386_watchdog_start();
981433d6423SLionel Sambuc #endif
982433d6423SLionel Sambuc 
983433d6423SLionel Sambuc 	return OK;
984433d6423SLionel Sambuc }
985433d6423SLionel Sambuc 
release_address_space(struct proc * pr)986433d6423SLionel Sambuc void release_address_space(struct proc *pr)
987433d6423SLionel Sambuc {
988433d6423SLionel Sambuc 	pr->p_seg.p_cr3_v = NULL;
989433d6423SLionel Sambuc }
990433d6423SLionel Sambuc 
991433d6423SLionel Sambuc /* computes a checksum of a buffer of a given length. The byte sum must be zero */
platform_tbl_checksum_ok(void * ptr,unsigned int length)992433d6423SLionel Sambuc int platform_tbl_checksum_ok(void *ptr, unsigned int length)
993433d6423SLionel Sambuc {
994433d6423SLionel Sambuc 	u8_t total = 0;
995433d6423SLionel Sambuc 	unsigned int i;
996433d6423SLionel Sambuc 	for (i = 0; i < length; i++)
997433d6423SLionel Sambuc 		total += ((unsigned char *)ptr)[i];
998433d6423SLionel Sambuc 	return !total;
999433d6423SLionel Sambuc }
1000433d6423SLionel Sambuc 
platform_tbl_ptr(phys_bytes start,phys_bytes end,unsigned increment,void * buff,unsigned size,phys_bytes * phys_addr,int ((* cmp_f)(void *)))1001433d6423SLionel Sambuc int platform_tbl_ptr(phys_bytes start,
1002433d6423SLionel Sambuc 					phys_bytes end,
1003433d6423SLionel Sambuc 					unsigned increment,
1004433d6423SLionel Sambuc 					void * buff,
1005433d6423SLionel Sambuc 					unsigned size,
1006433d6423SLionel Sambuc 					phys_bytes * phys_addr,
1007433d6423SLionel Sambuc 					int ((* cmp_f)(void *)))
1008433d6423SLionel Sambuc {
1009433d6423SLionel Sambuc 	phys_bytes addr;
1010433d6423SLionel Sambuc 
1011433d6423SLionel Sambuc 	for (addr = start; addr < end; addr += increment) {
1012433d6423SLionel Sambuc 		phys_copy (addr, (phys_bytes) buff, size);
1013433d6423SLionel Sambuc 		if (cmp_f(buff)) {
1014433d6423SLionel Sambuc 			if (phys_addr)
1015433d6423SLionel Sambuc 				*phys_addr = addr;
1016433d6423SLionel Sambuc 			return 1;
1017433d6423SLionel Sambuc 		}
1018433d6423SLionel Sambuc 	}
1019433d6423SLionel Sambuc 	return 0;
1020433d6423SLionel Sambuc }
1021