xref: /minix3/minix/kernel/arch/earm/memory.c (revision 59f1f7ecdd212fe00e1859dc950db6397e96abad)
1433d6423SLionel Sambuc 
2433d6423SLionel Sambuc #include "kernel/kernel.h"
3433d6423SLionel Sambuc #include "kernel/proc.h"
4433d6423SLionel Sambuc #include "kernel/vm.h"
5433d6423SLionel Sambuc 
6433d6423SLionel Sambuc #include <machine/vm.h>
7433d6423SLionel Sambuc 
8433d6423SLionel Sambuc #include <minix/type.h>
9433d6423SLionel Sambuc #include <minix/board.h>
10433d6423SLionel Sambuc #include <minix/syslib.h>
11433d6423SLionel Sambuc #include <minix/cpufeature.h>
12433d6423SLionel Sambuc #include <string.h>
13433d6423SLionel Sambuc #include <assert.h>
14433d6423SLionel Sambuc #include <signal.h>
15433d6423SLionel Sambuc #include <stdlib.h>
16433d6423SLionel Sambuc 
17433d6423SLionel Sambuc #include <machine/vm.h>
18433d6423SLionel Sambuc 
19433d6423SLionel Sambuc #include "arch_proto.h"
20433d6423SLionel Sambuc #include "kernel/proto.h"
21433d6423SLionel Sambuc #include "kernel/debug.h"
22433d6423SLionel Sambuc #include "bsp_timer.h"
23433d6423SLionel Sambuc 
24433d6423SLionel Sambuc 
25433d6423SLionel Sambuc #define HASPT(procptr) ((procptr)->p_seg.p_ttbr != 0)
26433d6423SLionel Sambuc static int nfreepdes = 0;
27433d6423SLionel Sambuc #define MAXFREEPDES	2
28433d6423SLionel Sambuc static int freepdes[MAXFREEPDES];
29433d6423SLionel Sambuc 
30433d6423SLionel Sambuc static u32_t phys_get32(phys_bytes v);
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc /* list of requested physical mapping */
33433d6423SLionel Sambuc static kern_phys_map *kern_phys_map_head;
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_ttbr_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 1MB windows.
52433d6423SLionel Sambuc  * I.e., it maps in 1MB chunks of virtual (or physical) address space
53433d6423SLionel Sambuc  * to 1MB 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 < 4096);
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_ttbr_v);
101433d6423SLionel Sambuc 		pdeval = pr->p_seg.p_ttbr_v[ARM_VM_PDE(linaddr)];
102433d6423SLionel Sambuc 	} else {
103433d6423SLionel Sambuc 		/* Requested address is physical. Make up the PDE entry. */
104433d6423SLionel Sambuc 		assert (linaddr >= PHYS_MEM_BEGIN && linaddr <= PHYS_MEM_END);
105433d6423SLionel Sambuc 
106433d6423SLionel Sambuc 		/* memory */
107433d6423SLionel Sambuc 		pdeval = (linaddr & ARM_VM_SECTION_MASK)
108433d6423SLionel Sambuc 			| ARM_VM_SECTION
109433d6423SLionel Sambuc 			| ARM_VM_SECTION_DOMAIN
110433d6423SLionel Sambuc 			| ARM_VM_SECTION_CACHED
111433d6423SLionel Sambuc 			| ARM_VM_SECTION_USER;
112433d6423SLionel Sambuc 	}
113433d6423SLionel Sambuc 
114433d6423SLionel Sambuc 	/* Write the pde value that we need into a pde that the kernel
115433d6423SLionel Sambuc 	 * can access, into the currently loaded page table so it becomes
116433d6423SLionel Sambuc 	 * visible.
117433d6423SLionel Sambuc 	 */
118433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
119433d6423SLionel Sambuc 	if(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v[pde] != pdeval) {
120433d6423SLionel Sambuc 		get_cpulocal_var(ptproc)->p_seg.p_ttbr_v[pde] = pdeval;
121433d6423SLionel Sambuc 		*changed = 1;
122433d6423SLionel Sambuc 	}
123433d6423SLionel Sambuc 
124433d6423SLionel Sambuc 	/* Memory is now available, but only the 1MB window of virtual
125433d6423SLionel Sambuc 	 * address space that we have mapped; calculate how much of
126433d6423SLionel Sambuc 	 * the requested range is visible and return that in *bytes,
127433d6423SLionel Sambuc 	 * if that is less than the requested range.
128433d6423SLionel Sambuc 	 */
129433d6423SLionel Sambuc 	offset = linaddr & ARM_VM_OFFSET_MASK_1MB; /* Offset in 1MB window. */
130433d6423SLionel Sambuc 	*bytes = MIN(*bytes, ARM_SECTION_SIZE - offset);
131433d6423SLionel Sambuc 
132433d6423SLionel Sambuc 	/* Return the linear address of the start of the new mapping. */
133433d6423SLionel Sambuc 	return ARM_SECTION_SIZE*pde + offset;
134433d6423SLionel Sambuc }
135433d6423SLionel Sambuc 
136433d6423SLionel Sambuc 
137433d6423SLionel Sambuc /*===========================================================================*
138433d6423SLionel Sambuc  *                           check_resumed_caller                            *
139433d6423SLionel Sambuc  *===========================================================================*/
check_resumed_caller(struct proc * caller)140433d6423SLionel Sambuc static int check_resumed_caller(struct proc *caller)
141433d6423SLionel Sambuc {
142433d6423SLionel Sambuc 	/* Returns the result from VM if caller was resumed, otherwise OK. */
143433d6423SLionel Sambuc 	if (caller && (caller->p_misc_flags & MF_KCALL_RESUME)) {
144433d6423SLionel Sambuc 		assert(caller->p_vmrequest.vmresult != VMSUSPEND);
145433d6423SLionel Sambuc 		return caller->p_vmrequest.vmresult;
146433d6423SLionel Sambuc 	}
147433d6423SLionel Sambuc 
148433d6423SLionel Sambuc 	return OK;
149433d6423SLionel Sambuc }
150433d6423SLionel Sambuc 
151433d6423SLionel Sambuc /*===========================================================================*
152433d6423SLionel Sambuc  *				lin_lin_copy				     *
153433d6423SLionel Sambuc  *===========================================================================*/
lin_lin_copy(struct proc * srcproc,vir_bytes srclinaddr,struct proc * dstproc,vir_bytes dstlinaddr,vir_bytes bytes)154433d6423SLionel Sambuc static int lin_lin_copy(struct proc *srcproc, vir_bytes srclinaddr,
155433d6423SLionel Sambuc 	struct proc *dstproc, vir_bytes dstlinaddr, vir_bytes bytes)
156433d6423SLionel Sambuc {
157433d6423SLionel Sambuc 	u32_t addr;
158433d6423SLionel Sambuc 	proc_nr_t procslot;
159433d6423SLionel Sambuc 
160433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc));
161433d6423SLionel Sambuc 	assert(get_cpulocal_var(proc_ptr));
162433d6423SLionel Sambuc 	assert(read_ttbr0() == get_cpulocal_var(ptproc)->p_seg.p_ttbr);
163433d6423SLionel Sambuc 
164433d6423SLionel Sambuc 	procslot = get_cpulocal_var(ptproc)->p_nr;
165433d6423SLionel Sambuc 
166433d6423SLionel Sambuc 	assert(procslot >= 0 && procslot < ARM_VM_DIR_ENTRIES);
167433d6423SLionel Sambuc 
168433d6423SLionel Sambuc 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
169433d6423SLionel Sambuc 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
170433d6423SLionel Sambuc 	assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
171433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
172433d6423SLionel Sambuc 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_VMINHIBIT));
173433d6423SLionel Sambuc 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_VMINHIBIT));
174433d6423SLionel Sambuc 
175433d6423SLionel Sambuc 	while(bytes > 0) {
176433d6423SLionel Sambuc 		phys_bytes srcptr, dstptr;
177433d6423SLionel Sambuc 		vir_bytes chunk = bytes;
178433d6423SLionel Sambuc 		int changed = 0;
179433d6423SLionel Sambuc 
180433d6423SLionel Sambuc #ifdef CONFIG_SMP
181433d6423SLionel Sambuc 		unsigned cpu = cpuid;
182433d6423SLionel Sambuc 
183433d6423SLionel Sambuc 		if (srcproc && GET_BIT(srcproc->p_stale_tlb, cpu)) {
184433d6423SLionel Sambuc 			changed = 1;
185433d6423SLionel Sambuc 			UNSET_BIT(srcproc->p_stale_tlb, cpu);
186433d6423SLionel Sambuc 		}
187433d6423SLionel Sambuc 		if (dstproc && GET_BIT(dstproc->p_stale_tlb, cpu)) {
188433d6423SLionel Sambuc 			changed = 1;
189433d6423SLionel Sambuc 			UNSET_BIT(dstproc->p_stale_tlb, cpu);
190433d6423SLionel Sambuc 		}
191433d6423SLionel Sambuc #endif
192433d6423SLionel Sambuc 
193433d6423SLionel Sambuc 		/* Set up 1MB ranges. */
194433d6423SLionel Sambuc 		srcptr = createpde(srcproc, srclinaddr, &chunk, 0, &changed);
195433d6423SLionel Sambuc 		dstptr = createpde(dstproc, dstlinaddr, &chunk, 1, &changed);
196*59f1f7ecSDavid van Moolenbroek 		if(changed)
197433d6423SLionel Sambuc 			reload_ttbr0();
198*59f1f7ecSDavid van Moolenbroek 
199*59f1f7ecSDavid van Moolenbroek 		/* Check for overflow. */
200*59f1f7ecSDavid van Moolenbroek 		if (srcptr + chunk < srcptr) return EFAULT_SRC;
201*59f1f7ecSDavid van Moolenbroek 		if (dstptr + chunk < dstptr) return EFAULT_DST;
202*59f1f7ecSDavid van Moolenbroek 
203433d6423SLionel Sambuc 		/* Copy pages. */
204433d6423SLionel Sambuc 		PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr);
205433d6423SLionel Sambuc 
206433d6423SLionel Sambuc 		if(addr) {
207433d6423SLionel Sambuc 			/* If addr is nonzero, a page fault was caught.
208433d6423SLionel Sambuc 			 *
209433d6423SLionel Sambuc 			 * phys_copy does all memory accesses word-aligned (rounded
210433d6423SLionel Sambuc 			 * down), so pagefaults can occur at a lower address than
211433d6423SLionel Sambuc 			 * the specified offsets. compute the lower bounds for sanity
212433d6423SLionel Sambuc 			 * check use.
213433d6423SLionel Sambuc 			 */
214433d6423SLionel Sambuc 			vir_bytes src_aligned = srcptr & ~0x3, dst_aligned = dstptr & ~0x3;
215433d6423SLionel Sambuc 
216433d6423SLionel Sambuc 			if(addr >= src_aligned && addr < (srcptr + chunk)) {
217433d6423SLionel Sambuc 				return EFAULT_SRC;
218433d6423SLionel Sambuc 			}
219433d6423SLionel Sambuc 			if(addr >= dst_aligned && addr < (dstptr + chunk)) {
220433d6423SLionel Sambuc 				return EFAULT_DST;
221433d6423SLionel Sambuc 			}
222433d6423SLionel Sambuc 
223433d6423SLionel Sambuc 			panic("lin_lin_copy fault out of range");
224433d6423SLionel Sambuc 
225433d6423SLionel Sambuc 			/* Not reached. */
226433d6423SLionel Sambuc 			return EFAULT;
227433d6423SLionel Sambuc 		}
228433d6423SLionel Sambuc 
229433d6423SLionel Sambuc 		/* Update counter and addresses for next iteration, if any. */
230433d6423SLionel Sambuc 		bytes -= chunk;
231433d6423SLionel Sambuc 		srclinaddr += chunk;
232433d6423SLionel Sambuc 		dstlinaddr += chunk;
233433d6423SLionel Sambuc 	}
234433d6423SLionel Sambuc 
235433d6423SLionel Sambuc 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
236433d6423SLionel Sambuc 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
237433d6423SLionel Sambuc 	assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
238433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
239433d6423SLionel Sambuc 
240433d6423SLionel Sambuc 	return OK;
241433d6423SLionel Sambuc }
242433d6423SLionel Sambuc 
phys_get32(phys_bytes addr)243433d6423SLionel Sambuc static u32_t phys_get32(phys_bytes addr)
244433d6423SLionel Sambuc {
245433d6423SLionel Sambuc 	u32_t v;
246433d6423SLionel Sambuc 	int r;
247433d6423SLionel Sambuc 
248433d6423SLionel Sambuc 	if((r=lin_lin_copy(NULL, addr,
249433d6423SLionel Sambuc 		proc_addr(SYSTEM), (phys_bytes) &v, sizeof(v))) != OK) {
250433d6423SLionel Sambuc 		panic("lin_lin_copy for phys_get32 failed: %d",  r);
251433d6423SLionel Sambuc 	}
252433d6423SLionel Sambuc 
253433d6423SLionel Sambuc 	return v;
254433d6423SLionel Sambuc }
255433d6423SLionel Sambuc 
256433d6423SLionel Sambuc /*===========================================================================*
257433d6423SLionel Sambuc  *                              umap_virtual                                 *
258433d6423SLionel Sambuc  *===========================================================================*/
umap_virtual(register struct proc * rp,int seg,vir_bytes vir_addr,vir_bytes bytes)2596077d1adSDr. Florian Grätz phys_bytes umap_virtual(
2606077d1adSDr. Florian Grätz   register struct proc *rp,		/* pointer to proc table entry for process */
2616077d1adSDr. Florian Grätz   int seg,				/* T, D, or S segment */
2626077d1adSDr. Florian Grätz   vir_bytes vir_addr,			/* virtual address in bytes within the seg */
2636077d1adSDr. Florian Grätz   vir_bytes bytes			/* # of bytes to be copied */
2646077d1adSDr. Florian Grätz )
265433d6423SLionel Sambuc {
266433d6423SLionel Sambuc 	phys_bytes phys = 0;
267433d6423SLionel Sambuc 
268433d6423SLionel Sambuc 	if(vm_lookup(rp, vir_addr, &phys, NULL) != OK) {
269433d6423SLionel Sambuc 		printf("SYSTEM:umap_virtual: vm_lookup of %s: seg 0x%x: 0x%lx failed\n", rp->p_name, seg, vir_addr);
270433d6423SLionel Sambuc 		phys = 0;
271433d6423SLionel Sambuc 	} else {
272433d6423SLionel Sambuc 		if(phys == 0)
273433d6423SLionel Sambuc 			panic("vm_lookup returned phys: 0x%lx",  phys);
274433d6423SLionel Sambuc 	}
275433d6423SLionel Sambuc 
276433d6423SLionel Sambuc 	if(phys == 0) {
277433d6423SLionel Sambuc 		printf("SYSTEM:umap_virtual: lookup failed\n");
278433d6423SLionel Sambuc 		return 0;
279433d6423SLionel Sambuc 	}
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc 	/* Now make sure addresses are contiguous in physical memory
282433d6423SLionel Sambuc 	 * so that the umap makes sense.
283433d6423SLionel Sambuc 	 */
284433d6423SLionel Sambuc 	if(bytes > 0 && vm_lookup_range(rp, vir_addr, NULL, bytes) != bytes) {
285433d6423SLionel Sambuc 		printf("umap_virtual: %s: %lu at 0x%lx (vir 0x%lx) not contiguous\n",
286433d6423SLionel Sambuc 			rp->p_name, bytes, vir_addr, vir_addr);
287433d6423SLionel Sambuc 		return 0;
288433d6423SLionel Sambuc 	}
289433d6423SLionel Sambuc 
290433d6423SLionel Sambuc 	/* phys must be larger than 0 (or the caller will think the call
291433d6423SLionel Sambuc 	 * failed), and address must not cross a page boundary.
292433d6423SLionel Sambuc 	 */
293433d6423SLionel Sambuc 	assert(phys);
294433d6423SLionel Sambuc 
295433d6423SLionel Sambuc 	return phys;
296433d6423SLionel Sambuc }
297433d6423SLionel Sambuc 
298433d6423SLionel Sambuc 
299433d6423SLionel Sambuc /*===========================================================================*
300433d6423SLionel Sambuc  *                              vm_lookup                                    *
301433d6423SLionel Sambuc  *===========================================================================*/
vm_lookup(const struct proc * proc,const vir_bytes virtual,phys_bytes * physical,u32_t * ptent)302433d6423SLionel Sambuc int vm_lookup(const struct proc *proc, const vir_bytes virtual,
303433d6423SLionel Sambuc  phys_bytes *physical, u32_t *ptent)
304433d6423SLionel Sambuc {
305433d6423SLionel Sambuc 	u32_t *root, *pt;
306433d6423SLionel Sambuc 	int pde, pte;
307433d6423SLionel Sambuc 	u32_t pde_v, pte_v;
308433d6423SLionel Sambuc 
309433d6423SLionel Sambuc 	assert(proc);
310433d6423SLionel Sambuc 	assert(physical);
311433d6423SLionel Sambuc 	assert(!isemptyp(proc));
312433d6423SLionel Sambuc 	assert(HASPT(proc));
313433d6423SLionel Sambuc 
314433d6423SLionel Sambuc 	/* Retrieve page directory entry. */
315433d6423SLionel Sambuc 	root = (u32_t *) (proc->p_seg.p_ttbr & ARM_TTBR_ADDR_MASK);
316433d6423SLionel Sambuc 	assert(!((u32_t) root % ARM_PAGEDIR_SIZE));
317433d6423SLionel Sambuc 	pde = ARM_VM_PDE(virtual);
318433d6423SLionel Sambuc 	assert(pde >= 0 && pde < ARM_VM_DIR_ENTRIES);
319433d6423SLionel Sambuc 	pde_v = phys_get32((u32_t) (root + pde));
320433d6423SLionel Sambuc 
321433d6423SLionel Sambuc 	if(! ((pde_v & ARM_VM_PDE_PRESENT)
322433d6423SLionel Sambuc 		|| (pde_v & ARM_VM_SECTION_PRESENT)
323433d6423SLionel Sambuc 	     )) {
324433d6423SLionel Sambuc 		return EFAULT;
325433d6423SLionel Sambuc 	}
326433d6423SLionel Sambuc 
327433d6423SLionel Sambuc 	if(pde_v & ARM_VM_SECTION) {
328433d6423SLionel Sambuc 		*physical = pde_v & ARM_VM_SECTION_MASK;
329433d6423SLionel Sambuc 		if(ptent) *ptent = pde_v;
330433d6423SLionel Sambuc 		*physical += virtual & ARM_VM_OFFSET_MASK_1MB;
331433d6423SLionel Sambuc 	} else  {
332433d6423SLionel Sambuc 		/* Retrieve page table entry. */
333433d6423SLionel Sambuc 		pt = (u32_t *) (pde_v & ARM_VM_PDE_MASK);
334433d6423SLionel Sambuc 		assert(!((u32_t) pt % ARM_PAGETABLE_SIZE));
335433d6423SLionel Sambuc 		pte = ARM_VM_PTE(virtual);
336433d6423SLionel Sambuc 		assert(pte >= 0 && pte < ARM_VM_PT_ENTRIES);
337433d6423SLionel Sambuc 		pte_v = phys_get32((u32_t) (pt + pte));
338433d6423SLionel Sambuc 		if(!(pte_v & ARM_VM_PTE_PRESENT)) {
339433d6423SLionel Sambuc 			return EFAULT;
340433d6423SLionel Sambuc 		}
341433d6423SLionel Sambuc 
342433d6423SLionel Sambuc 		if(ptent) *ptent = pte_v;
343433d6423SLionel Sambuc 
344433d6423SLionel Sambuc 		/* Actual address now known; retrieve it and add page offset. */
345433d6423SLionel Sambuc 		*physical = pte_v & ARM_VM_PTE_MASK;
346433d6423SLionel Sambuc 		*physical += virtual % ARM_PAGE_SIZE;
347433d6423SLionel Sambuc 	}
348433d6423SLionel Sambuc 
349433d6423SLionel Sambuc 	return OK;
350433d6423SLionel Sambuc }
351433d6423SLionel Sambuc 
352433d6423SLionel Sambuc /*===========================================================================*
353433d6423SLionel Sambuc  *				vm_lookup_range				     *
354433d6423SLionel Sambuc  *===========================================================================*/
vm_lookup_range(const struct proc * proc,vir_bytes vir_addr,phys_bytes * phys_addr,size_t bytes)355433d6423SLionel Sambuc size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr,
356433d6423SLionel Sambuc 	phys_bytes *phys_addr, size_t bytes)
357433d6423SLionel Sambuc {
358433d6423SLionel Sambuc 	/* Look up the physical address corresponding to linear virtual address
359433d6423SLionel Sambuc 	 * 'vir_addr' for process 'proc'. Return the size of the range covered
360433d6423SLionel Sambuc 	 * by contiguous physical memory starting from that address; this may
361433d6423SLionel Sambuc 	 * be anywhere between 0 and 'bytes' inclusive. If the return value is
362433d6423SLionel Sambuc 	 * nonzero, and 'phys_addr' is non-NULL, 'phys_addr' will be set to the
363433d6423SLionel Sambuc 	 * base physical address of the range. 'vir_addr' and 'bytes' need not
364433d6423SLionel Sambuc 	 * be page-aligned, but the caller must have verified that the given
365433d6423SLionel Sambuc 	 * linear range is valid for the given process at all.
366433d6423SLionel Sambuc 	 */
367433d6423SLionel Sambuc 	phys_bytes phys, next_phys;
368433d6423SLionel Sambuc 	size_t len;
369433d6423SLionel Sambuc 
370433d6423SLionel Sambuc 	assert(proc);
371433d6423SLionel Sambuc 	assert(bytes > 0);
372433d6423SLionel Sambuc 	assert(HASPT(proc));
373433d6423SLionel Sambuc 
374433d6423SLionel Sambuc 	/* Look up the first page. */
375433d6423SLionel Sambuc 	if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
376433d6423SLionel Sambuc 		return 0;
377433d6423SLionel Sambuc 
378433d6423SLionel Sambuc 	if (phys_addr != NULL)
379433d6423SLionel Sambuc 		*phys_addr = phys;
380433d6423SLionel Sambuc 
381433d6423SLionel Sambuc 	len = ARM_PAGE_SIZE - (vir_addr % ARM_PAGE_SIZE);
382433d6423SLionel Sambuc 	vir_addr += len;
383433d6423SLionel Sambuc 	next_phys = phys + len;
384433d6423SLionel Sambuc 
385433d6423SLionel Sambuc 	/* Look up any next pages and test physical contiguity. */
386433d6423SLionel Sambuc 	while (len < bytes) {
387433d6423SLionel Sambuc 		if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
388433d6423SLionel Sambuc 			break;
389433d6423SLionel Sambuc 
390433d6423SLionel Sambuc 		if (next_phys != phys)
391433d6423SLionel Sambuc 			break;
392433d6423SLionel Sambuc 
393433d6423SLionel Sambuc 		len += ARM_PAGE_SIZE;
394433d6423SLionel Sambuc 		vir_addr += ARM_PAGE_SIZE;
395433d6423SLionel Sambuc 		next_phys += ARM_PAGE_SIZE;
396433d6423SLionel Sambuc 	}
397433d6423SLionel Sambuc 
398433d6423SLionel Sambuc 	/* We might now have overshot the requested length somewhat. */
399433d6423SLionel Sambuc 	return MIN(bytes, len);
400433d6423SLionel Sambuc }
401433d6423SLionel Sambuc 
402433d6423SLionel Sambuc /*===========================================================================*
403433d6423SLionel Sambuc  *				vm_check_range				     *
404433d6423SLionel Sambuc  *===========================================================================*/
vm_check_range(struct proc * caller,struct proc * target,vir_bytes vir_addr,size_t bytes,int writeflag)405433d6423SLionel Sambuc int vm_check_range(struct proc *caller, struct proc *target,
406433d6423SLionel Sambuc 	vir_bytes vir_addr, size_t bytes, int writeflag)
407433d6423SLionel Sambuc {
408433d6423SLionel Sambuc 	/* Public interface to vm_suspend(), for use by kernel calls. On behalf
409433d6423SLionel Sambuc 	 * of 'caller', call into VM to check linear virtual address range of
410433d6423SLionel Sambuc 	 * process 'target', starting at 'vir_addr', for 'bytes' bytes. This
411433d6423SLionel Sambuc 	 * function assumes that it will called twice if VM returned an error
412433d6423SLionel Sambuc 	 * the first time (since nothing has changed in that case), and will
413433d6423SLionel Sambuc 	 * then return the error code resulting from the first call. Upon the
414433d6423SLionel Sambuc 	 * first call, a non-success error code is returned as well.
415433d6423SLionel Sambuc 	 */
416433d6423SLionel Sambuc 	int r;
417433d6423SLionel Sambuc 
418433d6423SLionel Sambuc 	if ((caller->p_misc_flags & MF_KCALL_RESUME) &&
419433d6423SLionel Sambuc 			(r = caller->p_vmrequest.vmresult) != OK)
420433d6423SLionel Sambuc 		return r;
421433d6423SLionel Sambuc 
422433d6423SLionel Sambuc 	vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL,
423433d6423SLionel Sambuc 		writeflag);
424433d6423SLionel Sambuc 
425433d6423SLionel Sambuc 	return VMSUSPEND;
426433d6423SLionel Sambuc }
427433d6423SLionel Sambuc 
428433d6423SLionel Sambuc /*===========================================================================*
429433d6423SLionel Sambuc  *                                 vmmemset                                  *
430433d6423SLionel Sambuc  *===========================================================================*/
vm_memset(struct proc * caller,endpoint_t who,phys_bytes ph,int c,phys_bytes count)431433d6423SLionel Sambuc int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c,
432433d6423SLionel Sambuc 	phys_bytes count)
433433d6423SLionel Sambuc {
434433d6423SLionel Sambuc 	u32_t pattern;
435433d6423SLionel Sambuc 	struct proc *whoptr = NULL;
436433d6423SLionel Sambuc 	phys_bytes cur_ph = ph;
437433d6423SLionel Sambuc 	phys_bytes left = count;
438433d6423SLionel Sambuc 	phys_bytes ptr, chunk, pfa = 0;
439433d6423SLionel Sambuc 	int new_ttbr, r = OK;
440433d6423SLionel Sambuc 
441433d6423SLionel Sambuc 	if ((r = check_resumed_caller(caller)) != OK)
442433d6423SLionel Sambuc 		return r;
443433d6423SLionel Sambuc 
444433d6423SLionel Sambuc 	/* NONE for physical, otherwise virtual */
445433d6423SLionel Sambuc 	if (who != NONE && !(whoptr = endpoint_lookup(who)))
446433d6423SLionel Sambuc 		return ESRCH;
447433d6423SLionel Sambuc 
448433d6423SLionel Sambuc 	c &= 0xFF;
449433d6423SLionel Sambuc 	pattern = c | (c << 8) | (c << 16) | (c << 24);
450433d6423SLionel Sambuc 
451433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
452433d6423SLionel Sambuc 	assert(!catch_pagefaults);
453433d6423SLionel Sambuc 	catch_pagefaults = 1;
454433d6423SLionel Sambuc 
455433d6423SLionel Sambuc 	/* We can memset as many bytes as we have remaining,
456433d6423SLionel Sambuc 	 * or as many as remain in the 1MB chunk we mapped in.
457433d6423SLionel Sambuc 	 */
458433d6423SLionel Sambuc 	while (left > 0) {
459433d6423SLionel Sambuc 		new_ttbr = 0;
460433d6423SLionel Sambuc 		chunk = left;
461433d6423SLionel Sambuc 		ptr = createpde(whoptr, cur_ph, &chunk, 0, &new_ttbr);
462433d6423SLionel Sambuc 
463433d6423SLionel Sambuc 		if (new_ttbr) {
464433d6423SLionel Sambuc 			reload_ttbr0();
465433d6423SLionel Sambuc 		}
466433d6423SLionel Sambuc 		/* If a page fault happens, pfa is non-null */
467433d6423SLionel Sambuc 		if ((pfa = phys_memset(ptr, pattern, chunk))) {
468433d6423SLionel Sambuc 
469433d6423SLionel Sambuc 			/* If a process pagefaults, VM may help out */
470433d6423SLionel Sambuc 			if (whoptr) {
471433d6423SLionel Sambuc 				vm_suspend(caller, whoptr, ph, count,
472433d6423SLionel Sambuc 						   VMSTYPE_KERNELCALL, 1);
473433d6423SLionel Sambuc 				assert(catch_pagefaults);
474433d6423SLionel Sambuc 				catch_pagefaults = 0;
475433d6423SLionel Sambuc 				return VMSUSPEND;
476433d6423SLionel Sambuc 			}
477433d6423SLionel Sambuc 
478433d6423SLionel Sambuc 			/* Pagefault when phys copying ?! */
479433d6423SLionel Sambuc 			panic("vm_memset: pf %lx addr=%lx len=%lu\n",
480433d6423SLionel Sambuc 						pfa , ptr, chunk);
481433d6423SLionel Sambuc 		}
482433d6423SLionel Sambuc 
483433d6423SLionel Sambuc 		cur_ph += chunk;
484433d6423SLionel Sambuc 		left -= chunk;
485433d6423SLionel Sambuc 	}
486433d6423SLionel Sambuc 
487433d6423SLionel Sambuc 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
488433d6423SLionel Sambuc 	assert(catch_pagefaults);
489433d6423SLionel Sambuc 	catch_pagefaults = 0;
490433d6423SLionel Sambuc 
491433d6423SLionel Sambuc 	return OK;
492433d6423SLionel Sambuc }
493433d6423SLionel Sambuc 
494433d6423SLionel Sambuc /*===========================================================================*
495433d6423SLionel Sambuc  *				virtual_copy_f				     *
496433d6423SLionel Sambuc  *===========================================================================*/
virtual_copy_f(struct proc * caller,struct vir_addr * src_addr,struct vir_addr * dst_addr,vir_bytes bytes,int vmcheck)4976077d1adSDr. Florian Grätz int virtual_copy_f(
4986077d1adSDr. Florian Grätz   struct proc * caller,
4996077d1adSDr. Florian Grätz   struct vir_addr *src_addr,		/* source virtual address */
5006077d1adSDr. Florian Grätz   struct vir_addr *dst_addr,		/* destination virtual address */
5016077d1adSDr. Florian Grätz   vir_bytes bytes,			/* # of bytes to copy  */
5026077d1adSDr. Florian Grätz   int vmcheck				/* if nonzero, can return VMSUSPEND */
5036077d1adSDr. Florian Grätz )
504433d6423SLionel Sambuc {
505433d6423SLionel Sambuc /* Copy bytes from virtual address src_addr to virtual address dst_addr. */
506433d6423SLionel Sambuc   struct vir_addr *vir_addr[2];	/* virtual source and destination address */
507433d6423SLionel Sambuc   int i, r;
508433d6423SLionel Sambuc   struct proc *procs[2];
509433d6423SLionel Sambuc 
510433d6423SLionel Sambuc   assert((vmcheck && caller) || (!vmcheck && !caller));
511433d6423SLionel Sambuc 
512433d6423SLionel Sambuc   /* Check copy count. */
513433d6423SLionel Sambuc   if (bytes <= 0) return(EDOM);
514433d6423SLionel Sambuc 
515433d6423SLionel Sambuc   /* Do some more checks and map virtual addresses to physical addresses. */
516433d6423SLionel Sambuc   vir_addr[_SRC_] = src_addr;
517433d6423SLionel Sambuc   vir_addr[_DST_] = dst_addr;
518433d6423SLionel Sambuc 
519433d6423SLionel Sambuc   for (i=_SRC_; i<=_DST_; i++) {
520433d6423SLionel Sambuc   	endpoint_t proc_e = vir_addr[i]->proc_nr_e;
521433d6423SLionel Sambuc 	int proc_nr;
522433d6423SLionel Sambuc 	struct proc *p;
523433d6423SLionel Sambuc 
524433d6423SLionel Sambuc 	if(proc_e == NONE) {
525433d6423SLionel Sambuc 		p = NULL;
526433d6423SLionel Sambuc 	} else {
527433d6423SLionel Sambuc 		if(!isokendpt(proc_e, &proc_nr)) {
528433d6423SLionel Sambuc 			printf("virtual_copy: no reasonable endpoint\n");
529433d6423SLionel Sambuc 			return ESRCH;
530433d6423SLionel Sambuc 		}
531433d6423SLionel Sambuc 		p = proc_addr(proc_nr);
532433d6423SLionel Sambuc 	}
533433d6423SLionel Sambuc 
534433d6423SLionel Sambuc 	procs[i] = p;
535433d6423SLionel Sambuc   }
536433d6423SLionel Sambuc 
537433d6423SLionel Sambuc   if ((r = check_resumed_caller(caller)) != OK)
538433d6423SLionel Sambuc 	return r;
539433d6423SLionel Sambuc 
540433d6423SLionel Sambuc   if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset,
541433d6423SLionel Sambuc   	procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) {
542433d6423SLionel Sambuc 	int writeflag;
543433d6423SLionel Sambuc   	struct proc *target = NULL;
544433d6423SLionel Sambuc   	phys_bytes lin;
545433d6423SLionel Sambuc   	if(r != EFAULT_SRC && r != EFAULT_DST)
546433d6423SLionel Sambuc   		panic("lin_lin_copy failed: %d",  r);
547433d6423SLionel Sambuc   	if(!vmcheck || !caller) {
548433d6423SLionel Sambuc     		return r;
549433d6423SLionel Sambuc   	}
550433d6423SLionel Sambuc 
551433d6423SLionel Sambuc   	if(r == EFAULT_SRC) {
552433d6423SLionel Sambuc   		lin = vir_addr[_SRC_]->offset;
553433d6423SLionel Sambuc   		target = procs[_SRC_];
554433d6423SLionel Sambuc 		writeflag = 0;
555433d6423SLionel Sambuc   	} else if(r == EFAULT_DST) {
556433d6423SLionel Sambuc   		lin = vir_addr[_DST_]->offset;
557433d6423SLionel Sambuc   		target = procs[_DST_];
558433d6423SLionel Sambuc 		writeflag = 1;
559433d6423SLionel Sambuc   	} else {
560433d6423SLionel Sambuc   		panic("r strange: %d",  r);
561433d6423SLionel Sambuc   	}
562433d6423SLionel Sambuc 
563433d6423SLionel Sambuc 	assert(caller);
564433d6423SLionel Sambuc 	assert(target);
565433d6423SLionel Sambuc 
566433d6423SLionel Sambuc 	vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag);
567433d6423SLionel Sambuc 	return VMSUSPEND;
568433d6423SLionel Sambuc   }
569433d6423SLionel Sambuc 
570433d6423SLionel Sambuc   return OK;
571433d6423SLionel Sambuc }
572433d6423SLionel Sambuc 
573433d6423SLionel Sambuc /*===========================================================================*
574433d6423SLionel Sambuc  *				data_copy				     *
575433d6423SLionel 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)576433d6423SLionel Sambuc int data_copy(const endpoint_t from_proc, const vir_bytes from_addr,
577433d6423SLionel Sambuc 	const endpoint_t to_proc, const vir_bytes to_addr,
578433d6423SLionel Sambuc 	size_t bytes)
579433d6423SLionel Sambuc {
580433d6423SLionel Sambuc   struct vir_addr src, dst;
581433d6423SLionel Sambuc 
582433d6423SLionel Sambuc   src.offset = from_addr;
583433d6423SLionel Sambuc   dst.offset = to_addr;
584433d6423SLionel Sambuc   src.proc_nr_e = from_proc;
585433d6423SLionel Sambuc   dst.proc_nr_e = to_proc;
586433d6423SLionel Sambuc   assert(src.proc_nr_e != NONE);
587433d6423SLionel Sambuc   assert(dst.proc_nr_e != NONE);
588433d6423SLionel Sambuc 
589433d6423SLionel Sambuc   return virtual_copy(&src, &dst, bytes);
590433d6423SLionel Sambuc }
591433d6423SLionel Sambuc 
592433d6423SLionel Sambuc /*===========================================================================*
593433d6423SLionel Sambuc  *				data_copy_vmcheck			     *
594433d6423SLionel 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)595433d6423SLionel Sambuc int data_copy_vmcheck(struct proc * caller,
596433d6423SLionel Sambuc 	const endpoint_t from_proc, const vir_bytes from_addr,
597433d6423SLionel Sambuc 	const endpoint_t to_proc, const vir_bytes to_addr,
598433d6423SLionel Sambuc 	size_t bytes)
599433d6423SLionel Sambuc {
600433d6423SLionel Sambuc   struct vir_addr src, dst;
601433d6423SLionel Sambuc 
602433d6423SLionel Sambuc   src.offset = from_addr;
603433d6423SLionel Sambuc   dst.offset = to_addr;
604433d6423SLionel Sambuc   src.proc_nr_e = from_proc;
605433d6423SLionel Sambuc   dst.proc_nr_e = to_proc;
606433d6423SLionel Sambuc   assert(src.proc_nr_e != NONE);
607433d6423SLionel Sambuc   assert(dst.proc_nr_e != NONE);
608433d6423SLionel Sambuc 
609433d6423SLionel Sambuc   return virtual_copy_vmcheck(caller, &src, &dst, bytes);
610433d6423SLionel Sambuc }
611433d6423SLionel Sambuc 
memory_init(void)612433d6423SLionel Sambuc void memory_init(void)
613433d6423SLionel Sambuc {
614433d6423SLionel Sambuc 	assert(nfreepdes == 0);
615433d6423SLionel Sambuc 
616433d6423SLionel Sambuc 	freepdes[nfreepdes++] = kinfo.freepde_start++;
617433d6423SLionel Sambuc 	freepdes[nfreepdes++] = kinfo.freepde_start++;
618433d6423SLionel Sambuc 
619433d6423SLionel Sambuc 	assert(kinfo.freepde_start < ARM_VM_DIR_ENTRIES);
620433d6423SLionel Sambuc 	assert(nfreepdes == 2);
621433d6423SLionel Sambuc 	assert(nfreepdes <= MAXFREEPDES);
622433d6423SLionel Sambuc }
623433d6423SLionel Sambuc 
624433d6423SLionel Sambuc /*===========================================================================*
625433d6423SLionel Sambuc  *				arch_proc_init				     *
626433d6423SLionel Sambuc  *===========================================================================*/
arch_proc_init(struct proc * pr,const u32_t ip,const u32_t sp,const u32_t ps_str,char * name)627433d6423SLionel Sambuc void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp,
628433d6423SLionel Sambuc 	const u32_t ps_str, char *name)
629433d6423SLionel Sambuc {
630433d6423SLionel Sambuc 	arch_proc_reset(pr);
631433d6423SLionel Sambuc 	strcpy(pr->p_name, name);
632433d6423SLionel Sambuc 
633433d6423SLionel Sambuc 	/* set custom state we know */
634433d6423SLionel Sambuc 	pr->p_reg.pc = ip;
635433d6423SLionel Sambuc 	pr->p_reg.sp = sp;
636433d6423SLionel Sambuc 	pr->p_reg.retreg = ps_str; /* a.k.a r0*/
637433d6423SLionel Sambuc }
638433d6423SLionel Sambuc 
639433d6423SLionel Sambuc static int usermapped_glo_index = -1,
640433d6423SLionel Sambuc 	usermapped_index = -1, first_um_idx = -1;
641433d6423SLionel Sambuc 
642433d6423SLionel Sambuc 
643433d6423SLionel Sambuc /* defined in kernel.lds */
644433d6423SLionel Sambuc extern char usermapped_start, usermapped_end, usermapped_nonglo_start;
645433d6423SLionel Sambuc 
arch_phys_map(const int index,phys_bytes * addr,phys_bytes * len,int * flags)646433d6423SLionel Sambuc int arch_phys_map(const int index,
647433d6423SLionel Sambuc 			phys_bytes *addr,
648433d6423SLionel Sambuc 			phys_bytes *len,
649433d6423SLionel Sambuc 			int *flags)
650433d6423SLionel Sambuc {
651433d6423SLionel Sambuc 	static int first = 1;
652433d6423SLionel Sambuc 	kern_phys_map *phys_maps;
653433d6423SLionel Sambuc 
654433d6423SLionel Sambuc 	int freeidx = 0;
655433d6423SLionel Sambuc 	u32_t glo_len = (u32_t) &usermapped_nonglo_start -
656433d6423SLionel Sambuc 			(u32_t) &usermapped_start;
657433d6423SLionel Sambuc 
658433d6423SLionel Sambuc 	if(first) {
659433d6423SLionel Sambuc 		memset(&minix_kerninfo, 0, sizeof(minix_kerninfo));
660433d6423SLionel Sambuc 		if(glo_len > 0) {
661433d6423SLionel Sambuc 			usermapped_glo_index = freeidx++;
662433d6423SLionel Sambuc 		}
663433d6423SLionel Sambuc 
664433d6423SLionel Sambuc 		usermapped_index = freeidx++;
665433d6423SLionel Sambuc 		first_um_idx = usermapped_index;
666433d6423SLionel Sambuc 		if(usermapped_glo_index != -1)
667433d6423SLionel Sambuc 			first_um_idx = usermapped_glo_index;
668433d6423SLionel Sambuc 		first = 0;
669433d6423SLionel Sambuc 
670433d6423SLionel Sambuc 		/* list over the maps and index them */
671433d6423SLionel Sambuc 		phys_maps = kern_phys_map_head;
672433d6423SLionel Sambuc 		while(phys_maps != NULL){
673433d6423SLionel Sambuc 			phys_maps->index = freeidx++;
674433d6423SLionel Sambuc 			phys_maps = phys_maps->next;
675433d6423SLionel Sambuc 		}
676433d6423SLionel Sambuc 
677433d6423SLionel Sambuc 	}
678433d6423SLionel Sambuc 
679433d6423SLionel Sambuc 	if(index == usermapped_glo_index) {
680433d6423SLionel Sambuc 		*addr = vir2phys(&usermapped_start);
681433d6423SLionel Sambuc 		*len = glo_len;
682433d6423SLionel Sambuc 		*flags = VMMF_USER | VMMF_GLO;
683433d6423SLionel Sambuc 		return OK;
684433d6423SLionel Sambuc 	}
685433d6423SLionel Sambuc 	else if(index == usermapped_index) {
686433d6423SLionel Sambuc 		*addr = vir2phys(&usermapped_nonglo_start);
687433d6423SLionel Sambuc 		*len = (u32_t) &usermapped_end -
688433d6423SLionel Sambuc 			(u32_t) &usermapped_nonglo_start;
689433d6423SLionel Sambuc 		*flags = VMMF_USER;
690433d6423SLionel Sambuc 		return OK;
691433d6423SLionel Sambuc 	}
692433d6423SLionel Sambuc 
693433d6423SLionel Sambuc 	/* if this all fails loop over the maps */
694433d6423SLionel Sambuc 	phys_maps = kern_phys_map_head;
695433d6423SLionel Sambuc 	while(phys_maps != NULL){
696433d6423SLionel Sambuc 		if(phys_maps->index == index){
697433d6423SLionel Sambuc 			*addr = phys_maps->addr;
698433d6423SLionel Sambuc 			*len =  phys_maps->size;
699433d6423SLionel Sambuc 			*flags = phys_maps->vm_flags;
700433d6423SLionel Sambuc 			return OK;
701433d6423SLionel Sambuc 		}
702433d6423SLionel Sambuc 		phys_maps = phys_maps->next;
703433d6423SLionel Sambuc 	}
704433d6423SLionel Sambuc 
705433d6423SLionel Sambuc 	return EINVAL;
706433d6423SLionel Sambuc }
707433d6423SLionel Sambuc 
arch_phys_map_reply(const int index,const vir_bytes addr)708433d6423SLionel Sambuc int arch_phys_map_reply(const int index, const vir_bytes addr)
709433d6423SLionel Sambuc {
710433d6423SLionel Sambuc 	kern_phys_map *phys_maps;
711433d6423SLionel Sambuc 
712433d6423SLionel Sambuc 	if(index == first_um_idx) {
713433d6423SLionel Sambuc 		u32_t usermapped_offset;
714433d6423SLionel Sambuc 		assert(addr > (u32_t) &usermapped_start);
715433d6423SLionel Sambuc 		usermapped_offset = addr - (u32_t) &usermapped_start;
716433d6423SLionel Sambuc #define FIXEDPTR(ptr) (void *) ((u32_t)ptr + usermapped_offset)
717433d6423SLionel Sambuc #define FIXPTR(ptr) ptr = FIXEDPTR(ptr)
718433d6423SLionel Sambuc #define ASSIGN(minixstruct) minix_kerninfo.minixstruct = FIXEDPTR(&minixstruct)
719433d6423SLionel Sambuc 		ASSIGN(kinfo);
720433d6423SLionel Sambuc 		ASSIGN(machine);
721433d6423SLionel Sambuc 		ASSIGN(kmessages);
722433d6423SLionel Sambuc 		ASSIGN(loadinfo);
72320054ae9SDavid van Moolenbroek 		ASSIGN(kuserinfo);
72426f5c8f8SDavid van Moolenbroek 		ASSIGN(arm_frclock);
725d91f738bSDavid van Moolenbroek 		ASSIGN(kclockinfo);
726433d6423SLionel Sambuc 
727433d6423SLionel Sambuc 		/* adjust the pointers of the functions and the struct
728433d6423SLionel Sambuc 		 * itself to the user-accessible mapping
729433d6423SLionel Sambuc 		 */
730433d6423SLionel Sambuc 		minix_kerninfo.kerninfo_magic = KERNINFO_MAGIC;
731433d6423SLionel Sambuc 		minix_kerninfo.minix_feature_flags = minix_feature_flags;
732433d6423SLionel Sambuc 		minix_kerninfo_user = (vir_bytes) FIXEDPTR(&minix_kerninfo);
73320054ae9SDavid van Moolenbroek 
73420054ae9SDavid van Moolenbroek 		minix_kerninfo.ki_flags |= MINIX_KIF_USERINFO;
73520054ae9SDavid van Moolenbroek 
736433d6423SLionel Sambuc 		return OK;
737433d6423SLionel Sambuc 	}
738433d6423SLionel Sambuc 
739433d6423SLionel Sambuc 	if (index == usermapped_index) {
740433d6423SLionel Sambuc 		return OK;
741433d6423SLionel Sambuc 	}
742433d6423SLionel Sambuc 
743433d6423SLionel Sambuc 	/* if this all fails loop over the maps */
744433d6423SLionel Sambuc 	/* list over the maps and index them */
745433d6423SLionel Sambuc 	phys_maps = kern_phys_map_head;
746433d6423SLionel Sambuc 	while(phys_maps != NULL){
747433d6423SLionel Sambuc 		if(phys_maps->index == index){
748433d6423SLionel Sambuc 			assert(phys_maps->cb != NULL);
749433d6423SLionel Sambuc 			/* only update the vir addr we are
750433d6423SLionel Sambuc 			   going to call the callback in enable
751433d6423SLionel Sambuc 			   paging
752433d6423SLionel Sambuc 			*/
753433d6423SLionel Sambuc 			phys_maps->vir = addr;
754433d6423SLionel Sambuc 			return OK;
755433d6423SLionel Sambuc 		}
756433d6423SLionel Sambuc 		phys_maps = phys_maps->next;
757433d6423SLionel Sambuc 	}
758433d6423SLionel Sambuc 
759433d6423SLionel Sambuc 	return EINVAL;
760433d6423SLionel Sambuc }
761433d6423SLionel Sambuc 
arch_enable_paging(struct proc * caller)762433d6423SLionel Sambuc int arch_enable_paging(struct proc * caller)
763433d6423SLionel Sambuc {
764433d6423SLionel Sambuc 	kern_phys_map *phys_maps;
765433d6423SLionel Sambuc 	assert(caller->p_seg.p_ttbr);
766433d6423SLionel Sambuc 
767433d6423SLionel Sambuc 
768433d6423SLionel Sambuc 	/* load caller's page table */
769433d6423SLionel Sambuc 	switch_address_space(caller);
770433d6423SLionel Sambuc 
771433d6423SLionel Sambuc 	/* We have now switched address spaces and the mappings are
772433d6423SLionel Sambuc 	   valid. We can now remap previous mappings. This is not a
773433d6423SLionel Sambuc 	   good time to do printf as the initial massing is gone and
774433d6423SLionel Sambuc 	   the new mapping is not in place */
775433d6423SLionel Sambuc 	phys_maps = kern_phys_map_head;
776433d6423SLionel Sambuc 	while(phys_maps != NULL){
777433d6423SLionel Sambuc 		assert(phys_maps->cb != NULL);
778433d6423SLionel Sambuc 		phys_maps->cb(phys_maps->id, phys_maps->vir);
779433d6423SLionel Sambuc 		phys_maps = phys_maps->next;
780433d6423SLionel Sambuc 	}
781433d6423SLionel Sambuc 
782433d6423SLionel Sambuc 	return OK;
783433d6423SLionel Sambuc }
784433d6423SLionel Sambuc 
release_address_space(struct proc * pr)785433d6423SLionel Sambuc void release_address_space(struct proc *pr)
786433d6423SLionel Sambuc {
787433d6423SLionel Sambuc 	pr->p_seg.p_ttbr_v = NULL;
788433d6423SLionel Sambuc 	barrier();
789433d6423SLionel Sambuc }
790433d6423SLionel Sambuc 
791433d6423SLionel Sambuc 
792433d6423SLionel Sambuc 
793433d6423SLionel Sambuc /*
794433d6423SLionel Sambuc  * Request a physical mapping
795433d6423SLionel Sambuc  */
kern_req_phys_map(phys_bytes base_address,vir_bytes io_size,int vm_flags,kern_phys_map * priv,kern_phys_map_mapped cb,vir_bytes id)796433d6423SLionel Sambuc int kern_req_phys_map( phys_bytes base_address, vir_bytes io_size,
797433d6423SLionel Sambuc 		       int vm_flags, kern_phys_map * priv,
798433d6423SLionel Sambuc 		       kern_phys_map_mapped cb, vir_bytes id)
799433d6423SLionel Sambuc {
800433d6423SLionel Sambuc 	/* Assign the values to the given struct and add priv
801433d6423SLionel Sambuc 	to the list */
802433d6423SLionel Sambuc 	assert(base_address != 0);
803433d6423SLionel Sambuc 	assert(io_size % ARM_PAGE_SIZE == 0);
804433d6423SLionel Sambuc 	assert(cb != NULL);
805433d6423SLionel Sambuc 
806433d6423SLionel Sambuc 	priv->addr  = base_address;
807433d6423SLionel Sambuc 	priv->size  = io_size;
808433d6423SLionel Sambuc 	priv->vm_flags  = vm_flags;
809433d6423SLionel Sambuc 	priv->cb  = cb;
810433d6423SLionel Sambuc 	priv->id  = id;
811433d6423SLionel Sambuc 	priv->index = -1;
812433d6423SLionel Sambuc 	priv->next = NULL;
813433d6423SLionel Sambuc 
814433d6423SLionel Sambuc 
815433d6423SLionel Sambuc 	if (kern_phys_map_head == NULL){
816433d6423SLionel Sambuc 		/* keep a list of items this is the first one */
817433d6423SLionel Sambuc 		kern_phys_map_head = priv;
818433d6423SLionel Sambuc 		kern_phys_map_head->next = NULL;
819433d6423SLionel Sambuc 	} else {
820433d6423SLionel Sambuc 		/* insert the item head but first keep track
821433d6423SLionel Sambuc 		   of the current by putting it in next */
822433d6423SLionel Sambuc 		priv->next = kern_phys_map_head;
823433d6423SLionel Sambuc 		/* replace the head */
824433d6423SLionel Sambuc 		kern_phys_map_head = priv;
825433d6423SLionel Sambuc 	}
826433d6423SLionel Sambuc 	return 0;
827433d6423SLionel Sambuc }
828433d6423SLionel Sambuc 
829433d6423SLionel Sambuc /*
830433d6423SLionel Sambuc  * Callback implementation where the id given to the
831433d6423SLionel Sambuc  * kern_phys_map is a pointer to the io map base address.
832433d6423SLionel Sambuc  * this implementation will just change that base address.
833433d6423SLionel Sambuc  * once that area is remapped.
834433d6423SLionel Sambuc  */
kern_phys_map_mapped_ptr(vir_bytes id,phys_bytes address)835433d6423SLionel Sambuc int kern_phys_map_mapped_ptr(vir_bytes id, phys_bytes address){
836433d6423SLionel Sambuc 	*((vir_bytes*)id) = address;
837433d6423SLionel Sambuc 	return 0;
838433d6423SLionel Sambuc }
839433d6423SLionel Sambuc 
840433d6423SLionel Sambuc /*
841433d6423SLionel Sambuc  * Request a physical mapping and put the result in the given prt
842433d6423SLionel Sambuc  * Note that ptr will only be valid once the callback happened.
843433d6423SLionel Sambuc  */
kern_phys_map_ptr(phys_bytes base_address,vir_bytes io_size,int vm_flags,kern_phys_map * priv,vir_bytes ptr)844433d6423SLionel Sambuc int kern_phys_map_ptr(
845433d6423SLionel Sambuc 	phys_bytes base_address,
846433d6423SLionel Sambuc 	vir_bytes io_size,
847433d6423SLionel Sambuc 	int vm_flags,
848433d6423SLionel Sambuc 	kern_phys_map * priv,
849433d6423SLionel Sambuc 	vir_bytes ptr)
850433d6423SLionel Sambuc {
851433d6423SLionel Sambuc 	return kern_req_phys_map(base_address,io_size,vm_flags,priv,kern_phys_map_mapped_ptr,ptr);
852433d6423SLionel Sambuc }
853433d6423SLionel Sambuc 
854