xref: /minix3/minix/kernel/arch/earm/memory.c (revision ace2de0ad1d8e59d59cc17c1a9b69743a509c2c6)
1 
2 #include "kernel/kernel.h"
3 #include "kernel/proc.h"
4 #include "kernel/vm.h"
5 
6 #include <machine/vm.h>
7 
8 #include <minix/type.h>
9 #include <minix/board.h>
10 #include <minix/syslib.h>
11 #include <minix/cpufeature.h>
12 #include <string.h>
13 #include <assert.h>
14 #include <signal.h>
15 #include <stdlib.h>
16 
17 #include <machine/vm.h>
18 
19 #include "arch_proto.h"
20 #include "kernel/proto.h"
21 #include "kernel/debug.h"
22 #include "bsp_timer.h"
23 
24 
25 #define HASPT(procptr) ((procptr)->p_seg.p_ttbr != 0)
26 static int nfreepdes = 0;
27 #define MAXFREEPDES	2
28 static int freepdes[MAXFREEPDES];
29 
30 static u32_t phys_get32(phys_bytes v);
31 
32 /* list of requested physical mapping */
33 static kern_phys_map *kern_phys_map_head;
34 
35 void mem_clear_mapcache(void)
36 {
37 	int i;
38 	for(i = 0; i < nfreepdes; i++) {
39 		struct proc *ptproc = get_cpulocal_var(ptproc);
40 		int pde = freepdes[i];
41 		u32_t *ptv;
42 		assert(ptproc);
43 		ptv = ptproc->p_seg.p_ttbr_v;
44 		assert(ptv);
45 		ptv[pde] = 0;
46 	}
47 }
48 
49 /* This function sets up a mapping from within the kernel's address
50  * space to any other area of memory, either straight physical
51  * memory (pr == NULL) or a process view of memory, in 1MB windows.
52  * I.e., it maps in 1MB chunks of virtual (or physical) address space
53  * to 1MB chunks of kernel virtual address space.
54  *
55  * It recognizes pr already being in memory as a special case (no
56  * mapping required).
57  *
58  * The target (i.e. in-kernel) mapping area is one of the freepdes[]
59  * VM has earlier already told the kernel about that is available. It is
60  * identified as the 'pde' parameter. This value can be chosen freely
61  * by the caller, as long as it is in range (i.e. 0 or higher and corresponds
62  * to a known freepde slot). It is up to the caller to keep track of which
63  * freepde's are in use, and to determine which ones are free to use.
64  *
65  * The logical number supplied by the caller is translated into an actual
66  * pde number to be used, and a pointer to it (linear address) is returned
67  * for actual use by phys_copy or memset.
68  */
69 static phys_bytes createpde(
70 	const struct proc *pr,	/* Requested process, NULL for physical. */
71 	const phys_bytes linaddr,/* Address after segment translation. */
72 	phys_bytes *bytes,	/* Size of chunk, function may truncate it. */
73 	int free_pde_idx,	/* index of the free slot to use */
74 	int *changed		/* If mapping is made, this is set to 1. */
75 	)
76 {
77 	u32_t pdeval;
78 	phys_bytes offset;
79 	int pde;
80 
81 	assert(free_pde_idx >= 0 && free_pde_idx < nfreepdes);
82 	pde = freepdes[free_pde_idx];
83 	assert(pde >= 0 && pde < 4096);
84 
85 	if(pr && ((pr == get_cpulocal_var(ptproc)) || iskernelp(pr))) {
86 		/* Process memory is requested, and
87 		 * it's a process that is already in current page table, or
88 		 * the kernel, which is always there.
89 		 * Therefore linaddr is valid directly, with the requested
90 		 * size.
91 		 */
92 		return linaddr;
93 	}
94 
95 	if(pr) {
96 		/* Requested address is in a process that is not currently
97 		 * accessible directly. Grab the PDE entry of that process'
98 		 * page table that corresponds to the requested address.
99 		 */
100 		assert(pr->p_seg.p_ttbr_v);
101 		pdeval = pr->p_seg.p_ttbr_v[ARM_VM_PDE(linaddr)];
102 	} else {
103 		/* Requested address is physical. Make up the PDE entry. */
104 		assert (linaddr >= PHYS_MEM_BEGIN && linaddr <= PHYS_MEM_END);
105 
106 		/* memory */
107 		pdeval = (linaddr & ARM_VM_SECTION_MASK)
108 			| ARM_VM_SECTION
109 			| ARM_VM_SECTION_DOMAIN
110 			| ARM_VM_SECTION_CACHED
111 			| ARM_VM_SECTION_USER;
112 	}
113 
114 	/* Write the pde value that we need into a pde that the kernel
115 	 * can access, into the currently loaded page table so it becomes
116 	 * visible.
117 	 */
118 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
119 	if(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v[pde] != pdeval) {
120 		get_cpulocal_var(ptproc)->p_seg.p_ttbr_v[pde] = pdeval;
121 		*changed = 1;
122 	}
123 
124 	/* Memory is now available, but only the 1MB window of virtual
125 	 * address space that we have mapped; calculate how much of
126 	 * the requested range is visible and return that in *bytes,
127 	 * if that is less than the requested range.
128 	 */
129 	offset = linaddr & ARM_VM_OFFSET_MASK_1MB; /* Offset in 1MB window. */
130 	*bytes = MIN(*bytes, ARM_SECTION_SIZE - offset);
131 
132 	/* Return the linear address of the start of the new mapping. */
133 	return ARM_SECTION_SIZE*pde + offset;
134 }
135 
136 
137 /*===========================================================================*
138  *                           check_resumed_caller                            *
139  *===========================================================================*/
140 static int check_resumed_caller(struct proc *caller)
141 {
142 	/* Returns the result from VM if caller was resumed, otherwise OK. */
143 	if (caller && (caller->p_misc_flags & MF_KCALL_RESUME)) {
144 		assert(caller->p_vmrequest.vmresult != VMSUSPEND);
145 		return caller->p_vmrequest.vmresult;
146 	}
147 
148 	return OK;
149 }
150 
151 /*===========================================================================*
152  *				lin_lin_copy				     *
153  *===========================================================================*/
154 static int lin_lin_copy(struct proc *srcproc, vir_bytes srclinaddr,
155 	struct proc *dstproc, vir_bytes dstlinaddr, vir_bytes bytes)
156 {
157 	u32_t addr;
158 	proc_nr_t procslot;
159 
160 	assert(get_cpulocal_var(ptproc));
161 	assert(get_cpulocal_var(proc_ptr));
162 	assert(read_ttbr0() == get_cpulocal_var(ptproc)->p_seg.p_ttbr);
163 
164 	procslot = get_cpulocal_var(ptproc)->p_nr;
165 
166 	assert(procslot >= 0 && procslot < ARM_VM_DIR_ENTRIES);
167 
168 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
169 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
170 	assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
171 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
172 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_VMINHIBIT));
173 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_VMINHIBIT));
174 
175 	while(bytes > 0) {
176 		phys_bytes srcptr, dstptr;
177 		vir_bytes chunk = bytes;
178 		int changed = 0;
179 
180 #ifdef CONFIG_SMP
181 		unsigned cpu = cpuid;
182 
183 		if (srcproc && GET_BIT(srcproc->p_stale_tlb, cpu)) {
184 			changed = 1;
185 			UNSET_BIT(srcproc->p_stale_tlb, cpu);
186 		}
187 		if (dstproc && GET_BIT(dstproc->p_stale_tlb, cpu)) {
188 			changed = 1;
189 			UNSET_BIT(dstproc->p_stale_tlb, cpu);
190 		}
191 #endif
192 
193 		/* Set up 1MB ranges. */
194 		srcptr = createpde(srcproc, srclinaddr, &chunk, 0, &changed);
195 		dstptr = createpde(dstproc, dstlinaddr, &chunk, 1, &changed);
196 		if(changed)
197 			reload_ttbr0();
198 
199 		/* Check for overflow. */
200 		if (srcptr + chunk < srcptr) return EFAULT_SRC;
201 		if (dstptr + chunk < dstptr) return EFAULT_DST;
202 
203 		/* Copy pages. */
204 		PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr);
205 
206 		if(addr) {
207 			/* If addr is nonzero, a page fault was caught.
208 			 *
209 			 * phys_copy does all memory accesses word-aligned (rounded
210 			 * down), so pagefaults can occur at a lower address than
211 			 * the specified offsets. compute the lower bounds for sanity
212 			 * check use.
213 			 */
214 			vir_bytes src_aligned = srcptr & ~0x3, dst_aligned = dstptr & ~0x3;
215 
216 			if(addr >= src_aligned && addr < (srcptr + chunk)) {
217 				return EFAULT_SRC;
218 			}
219 			if(addr >= dst_aligned && addr < (dstptr + chunk)) {
220 				return EFAULT_DST;
221 			}
222 
223 			panic("lin_lin_copy fault out of range");
224 
225 			/* Not reached. */
226 			return EFAULT;
227 		}
228 
229 		/* Update counter and addresses for next iteration, if any. */
230 		bytes -= chunk;
231 		srclinaddr += chunk;
232 		dstlinaddr += chunk;
233 	}
234 
235 	if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE));
236 	if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE));
237 	assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE));
238 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
239 
240 	return OK;
241 }
242 
243 static u32_t phys_get32(phys_bytes addr)
244 {
245 	u32_t v;
246 	int r;
247 
248 	if((r=lin_lin_copy(NULL, addr,
249 		proc_addr(SYSTEM), (phys_bytes) &v, sizeof(v))) != OK) {
250 		panic("lin_lin_copy for phys_get32 failed: %d",  r);
251 	}
252 
253 	return v;
254 }
255 
256 /*===========================================================================*
257  *                              umap_virtual                                 *
258  *===========================================================================*/
259 phys_bytes umap_virtual(
260   register struct proc *rp,		/* pointer to proc table entry for process */
261   int seg,				/* T, D, or S segment */
262   vir_bytes vir_addr,			/* virtual address in bytes within the seg */
263   vir_bytes bytes			/* # of bytes to be copied */
264 )
265 {
266 	phys_bytes phys = 0;
267 
268 	if(vm_lookup(rp, vir_addr, &phys, NULL) != OK) {
269 		printf("SYSTEM:umap_virtual: vm_lookup of %s: seg 0x%x: 0x%lx failed\n", rp->p_name, seg, vir_addr);
270 		phys = 0;
271 	} else {
272 		if(phys == 0)
273 			panic("vm_lookup returned phys: 0x%lx",  phys);
274 	}
275 
276 	if(phys == 0) {
277 		printf("SYSTEM:umap_virtual: lookup failed\n");
278 		return 0;
279 	}
280 
281 	/* Now make sure addresses are contiguous in physical memory
282 	 * so that the umap makes sense.
283 	 */
284 	if(bytes > 0 && vm_lookup_range(rp, vir_addr, NULL, bytes) != bytes) {
285 		printf("umap_virtual: %s: %lu at 0x%lx (vir 0x%lx) not contiguous\n",
286 			rp->p_name, bytes, vir_addr, vir_addr);
287 		return 0;
288 	}
289 
290 	/* phys must be larger than 0 (or the caller will think the call
291 	 * failed), and address must not cross a page boundary.
292 	 */
293 	assert(phys);
294 
295 	return phys;
296 }
297 
298 
299 /*===========================================================================*
300  *                              vm_lookup                                    *
301  *===========================================================================*/
302 int vm_lookup(const struct proc *proc, const vir_bytes virtual,
303  phys_bytes *physical, u32_t *ptent)
304 {
305 	u32_t *root, *pt;
306 	int pde, pte;
307 	u32_t pde_v, pte_v;
308 
309 	assert(proc);
310 	assert(physical);
311 	assert(!isemptyp(proc));
312 	assert(HASPT(proc));
313 
314 	/* Retrieve page directory entry. */
315 	root = (u32_t *) (proc->p_seg.p_ttbr & ARM_TTBR_ADDR_MASK);
316 	assert(!((u32_t) root % ARM_PAGEDIR_SIZE));
317 	pde = ARM_VM_PDE(virtual);
318 	assert(pde >= 0 && pde < ARM_VM_DIR_ENTRIES);
319 	pde_v = phys_get32((u32_t) (root + pde));
320 
321 	if(! ((pde_v & ARM_VM_PDE_PRESENT)
322 		|| (pde_v & ARM_VM_SECTION_PRESENT)
323 	     )) {
324 		return EFAULT;
325 	}
326 
327 	if(pde_v & ARM_VM_SECTION) {
328 		*physical = pde_v & ARM_VM_SECTION_MASK;
329 		if(ptent) *ptent = pde_v;
330 		*physical += virtual & ARM_VM_OFFSET_MASK_1MB;
331 	} else  {
332 		/* Retrieve page table entry. */
333 		pt = (u32_t *) (pde_v & ARM_VM_PDE_MASK);
334 		assert(!((u32_t) pt % ARM_PAGETABLE_SIZE));
335 		pte = ARM_VM_PTE(virtual);
336 		assert(pte >= 0 && pte < ARM_VM_PT_ENTRIES);
337 		pte_v = phys_get32((u32_t) (pt + pte));
338 		if(!(pte_v & ARM_VM_PTE_PRESENT)) {
339 			return EFAULT;
340 		}
341 
342 		if(ptent) *ptent = pte_v;
343 
344 		/* Actual address now known; retrieve it and add page offset. */
345 		*physical = pte_v & ARM_VM_PTE_MASK;
346 		*physical += virtual % ARM_PAGE_SIZE;
347 	}
348 
349 	return OK;
350 }
351 
352 /*===========================================================================*
353  *				vm_lookup_range				     *
354  *===========================================================================*/
355 size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr,
356 	phys_bytes *phys_addr, size_t bytes)
357 {
358 	/* Look up the physical address corresponding to linear virtual address
359 	 * 'vir_addr' for process 'proc'. Return the size of the range covered
360 	 * by contiguous physical memory starting from that address; this may
361 	 * be anywhere between 0 and 'bytes' inclusive. If the return value is
362 	 * nonzero, and 'phys_addr' is non-NULL, 'phys_addr' will be set to the
363 	 * base physical address of the range. 'vir_addr' and 'bytes' need not
364 	 * be page-aligned, but the caller must have verified that the given
365 	 * linear range is valid for the given process at all.
366 	 */
367 	phys_bytes phys, next_phys;
368 	size_t len;
369 
370 	assert(proc);
371 	assert(bytes > 0);
372 	assert(HASPT(proc));
373 
374 	/* Look up the first page. */
375 	if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
376 		return 0;
377 
378 	if (phys_addr != NULL)
379 		*phys_addr = phys;
380 
381 	len = ARM_PAGE_SIZE - (vir_addr % ARM_PAGE_SIZE);
382 	vir_addr += len;
383 	next_phys = phys + len;
384 
385 	/* Look up any next pages and test physical contiguity. */
386 	while (len < bytes) {
387 		if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
388 			break;
389 
390 		if (next_phys != phys)
391 			break;
392 
393 		len += ARM_PAGE_SIZE;
394 		vir_addr += ARM_PAGE_SIZE;
395 		next_phys += ARM_PAGE_SIZE;
396 	}
397 
398 	/* We might now have overshot the requested length somewhat. */
399 	return MIN(bytes, len);
400 }
401 
402 /*===========================================================================*
403  *				vm_check_range				     *
404  *===========================================================================*/
405 int vm_check_range(struct proc *caller, struct proc *target,
406 	vir_bytes vir_addr, size_t bytes, int writeflag)
407 {
408 	/* Public interface to vm_suspend(), for use by kernel calls. On behalf
409 	 * of 'caller', call into VM to check linear virtual address range of
410 	 * process 'target', starting at 'vir_addr', for 'bytes' bytes. This
411 	 * function assumes that it will called twice if VM returned an error
412 	 * the first time (since nothing has changed in that case), and will
413 	 * then return the error code resulting from the first call. Upon the
414 	 * first call, a non-success error code is returned as well.
415 	 */
416 	int r;
417 
418 	if ((caller->p_misc_flags & MF_KCALL_RESUME) &&
419 			(r = caller->p_vmrequest.vmresult) != OK)
420 		return r;
421 
422 	vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL,
423 		writeflag);
424 
425 	return VMSUSPEND;
426 }
427 
428 /*===========================================================================*
429  *                                 vmmemset                                  *
430  *===========================================================================*/
431 int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c,
432 	phys_bytes count)
433 {
434 	u32_t pattern;
435 	struct proc *whoptr = NULL;
436 	phys_bytes cur_ph = ph;
437 	phys_bytes left = count;
438 	phys_bytes ptr, chunk, pfa = 0;
439 	int new_ttbr, r = OK;
440 
441 	if ((r = check_resumed_caller(caller)) != OK)
442 		return r;
443 
444 	/* NONE for physical, otherwise virtual */
445 	if (who != NONE && !(whoptr = endpoint_lookup(who)))
446 		return ESRCH;
447 
448 	c &= 0xFF;
449 	pattern = c | (c << 8) | (c << 16) | (c << 24);
450 
451 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
452 	assert(!catch_pagefaults);
453 	catch_pagefaults = 1;
454 
455 	/* We can memset as many bytes as we have remaining,
456 	 * or as many as remain in the 1MB chunk we mapped in.
457 	 */
458 	while (left > 0) {
459 		new_ttbr = 0;
460 		chunk = left;
461 		ptr = createpde(whoptr, cur_ph, &chunk, 0, &new_ttbr);
462 
463 		if (new_ttbr) {
464 			reload_ttbr0();
465 		}
466 		/* If a page fault happens, pfa is non-null */
467 		if ((pfa = phys_memset(ptr, pattern, chunk))) {
468 
469 			/* If a process pagefaults, VM may help out */
470 			if (whoptr) {
471 				vm_suspend(caller, whoptr, ph, count,
472 						   VMSTYPE_KERNELCALL, 1);
473 				assert(catch_pagefaults);
474 				catch_pagefaults = 0;
475 				return VMSUSPEND;
476 			}
477 
478 			/* Pagefault when phys copying ?! */
479 			panic("vm_memset: pf %lx addr=%lx len=%lu\n",
480 						pfa , ptr, chunk);
481 		}
482 
483 		cur_ph += chunk;
484 		left -= chunk;
485 	}
486 
487 	assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
488 	assert(catch_pagefaults);
489 	catch_pagefaults = 0;
490 
491 	return OK;
492 }
493 
494 /*===========================================================================*
495  *				virtual_copy_f				     *
496  *===========================================================================*/
497 int virtual_copy_f(
498   struct proc * caller,
499   struct vir_addr *src_addr,		/* source virtual address */
500   struct vir_addr *dst_addr,		/* destination virtual address */
501   vir_bytes bytes,			/* # of bytes to copy  */
502   int vmcheck				/* if nonzero, can return VMSUSPEND */
503 )
504 {
505 /* Copy bytes from virtual address src_addr to virtual address dst_addr. */
506   struct vir_addr *vir_addr[2];	/* virtual source and destination address */
507   int i, r;
508   struct proc *procs[2];
509 
510   assert((vmcheck && caller) || (!vmcheck && !caller));
511 
512   /* Check copy count. */
513   if (bytes <= 0) return(EDOM);
514 
515   /* Do some more checks and map virtual addresses to physical addresses. */
516   vir_addr[_SRC_] = src_addr;
517   vir_addr[_DST_] = dst_addr;
518 
519   for (i=_SRC_; i<=_DST_; i++) {
520   	endpoint_t proc_e = vir_addr[i]->proc_nr_e;
521 	int proc_nr;
522 	struct proc *p;
523 
524 	if(proc_e == NONE) {
525 		p = NULL;
526 	} else {
527 		if(!isokendpt(proc_e, &proc_nr)) {
528 			printf("virtual_copy: no reasonable endpoint\n");
529 			return ESRCH;
530 		}
531 		p = proc_addr(proc_nr);
532 	}
533 
534 	procs[i] = p;
535   }
536 
537   if ((r = check_resumed_caller(caller)) != OK)
538 	return r;
539 
540   if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset,
541   	procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) {
542 	int writeflag;
543   	struct proc *target = NULL;
544   	phys_bytes lin;
545   	if(r != EFAULT_SRC && r != EFAULT_DST)
546   		panic("lin_lin_copy failed: %d",  r);
547   	if(!vmcheck || !caller) {
548     		return r;
549   	}
550 
551   	if(r == EFAULT_SRC) {
552   		lin = vir_addr[_SRC_]->offset;
553   		target = procs[_SRC_];
554 		writeflag = 0;
555   	} else if(r == EFAULT_DST) {
556   		lin = vir_addr[_DST_]->offset;
557   		target = procs[_DST_];
558 		writeflag = 1;
559   	} else {
560   		panic("r strange: %d",  r);
561   	}
562 
563 	assert(caller);
564 	assert(target);
565 
566 	vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag);
567 	return VMSUSPEND;
568   }
569 
570   return OK;
571 }
572 
573 /*===========================================================================*
574  *				data_copy				     *
575  *===========================================================================*/
576 int data_copy(const endpoint_t from_proc, const vir_bytes from_addr,
577 	const endpoint_t to_proc, const vir_bytes to_addr,
578 	size_t bytes)
579 {
580   struct vir_addr src, dst;
581 
582   src.offset = from_addr;
583   dst.offset = to_addr;
584   src.proc_nr_e = from_proc;
585   dst.proc_nr_e = to_proc;
586   assert(src.proc_nr_e != NONE);
587   assert(dst.proc_nr_e != NONE);
588 
589   return virtual_copy(&src, &dst, bytes);
590 }
591 
592 /*===========================================================================*
593  *				data_copy_vmcheck			     *
594  *===========================================================================*/
595 int data_copy_vmcheck(struct proc * caller,
596 	const endpoint_t from_proc, const vir_bytes from_addr,
597 	const endpoint_t to_proc, const vir_bytes to_addr,
598 	size_t bytes)
599 {
600   struct vir_addr src, dst;
601 
602   src.offset = from_addr;
603   dst.offset = to_addr;
604   src.proc_nr_e = from_proc;
605   dst.proc_nr_e = to_proc;
606   assert(src.proc_nr_e != NONE);
607   assert(dst.proc_nr_e != NONE);
608 
609   return virtual_copy_vmcheck(caller, &src, &dst, bytes);
610 }
611 
612 void memory_init(void)
613 {
614 	assert(nfreepdes == 0);
615 
616 	freepdes[nfreepdes++] = kinfo.freepde_start++;
617 	freepdes[nfreepdes++] = kinfo.freepde_start++;
618 
619 	assert(kinfo.freepde_start < ARM_VM_DIR_ENTRIES);
620 	assert(nfreepdes == 2);
621 	assert(nfreepdes <= MAXFREEPDES);
622 }
623 
624 /*===========================================================================*
625  *				arch_proc_init				     *
626  *===========================================================================*/
627 void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp,
628 	const u32_t ps_str, char *name)
629 {
630 	arch_proc_reset(pr);
631 	strcpy(pr->p_name, name);
632 
633 	/* set custom state we know */
634 	pr->p_reg.pc = ip;
635 	pr->p_reg.sp = sp;
636 	pr->p_reg.retreg = ps_str; /* a.k.a r0*/
637 }
638 
639 static int usermapped_glo_index = -1,
640 	usermapped_index = -1, first_um_idx = -1;
641 
642 
643 /* defined in kernel.lds */
644 extern char usermapped_start, usermapped_end, usermapped_nonglo_start;
645 
646 int arch_phys_map(const int index,
647 			phys_bytes *addr,
648 			phys_bytes *len,
649 			int *flags)
650 {
651 	static int first = 1;
652 	kern_phys_map *phys_maps;
653 
654 	int freeidx = 0;
655 	u32_t glo_len = (u32_t) &usermapped_nonglo_start -
656 			(u32_t) &usermapped_start;
657 
658 	if(first) {
659 		memset(&minix_kerninfo, 0, sizeof(minix_kerninfo));
660 		if(glo_len > 0) {
661 			usermapped_glo_index = freeidx++;
662 		}
663 
664 		usermapped_index = freeidx++;
665 		first_um_idx = usermapped_index;
666 		if(usermapped_glo_index != -1)
667 			first_um_idx = usermapped_glo_index;
668 		first = 0;
669 
670 		/* list over the maps and index them */
671 		phys_maps = kern_phys_map_head;
672 		while(phys_maps != NULL){
673 			phys_maps->index = freeidx++;
674 			phys_maps = phys_maps->next;
675 		}
676 
677 	}
678 
679 	if(index == usermapped_glo_index) {
680 		*addr = vir2phys(&usermapped_start);
681 		*len = glo_len;
682 		*flags = VMMF_USER | VMMF_GLO;
683 		return OK;
684 	}
685 	else if(index == usermapped_index) {
686 		*addr = vir2phys(&usermapped_nonglo_start);
687 		*len = (u32_t) &usermapped_end -
688 			(u32_t) &usermapped_nonglo_start;
689 		*flags = VMMF_USER;
690 		return OK;
691 	}
692 
693 	/* if this all fails loop over the maps */
694 	phys_maps = kern_phys_map_head;
695 	while(phys_maps != NULL){
696 		if(phys_maps->index == index){
697 			*addr = phys_maps->addr;
698 			*len =  phys_maps->size;
699 			*flags = phys_maps->vm_flags;
700 			return OK;
701 		}
702 		phys_maps = phys_maps->next;
703 	}
704 
705 	return EINVAL;
706 }
707 
708 int arch_phys_map_reply(const int index, const vir_bytes addr)
709 {
710 	kern_phys_map *phys_maps;
711 
712 	if(index == first_um_idx) {
713 		u32_t usermapped_offset;
714 		assert(addr > (u32_t) &usermapped_start);
715 		usermapped_offset = addr - (u32_t) &usermapped_start;
716 #define FIXEDPTR(ptr) (void *) ((u32_t)ptr + usermapped_offset)
717 #define FIXPTR(ptr) ptr = FIXEDPTR(ptr)
718 #define ASSIGN(minixstruct) minix_kerninfo.minixstruct = FIXEDPTR(&minixstruct)
719 		ASSIGN(kinfo);
720 		ASSIGN(machine);
721 		ASSIGN(kmessages);
722 		ASSIGN(loadinfo);
723 		ASSIGN(kuserinfo);
724 		ASSIGN(arm_frclock);
725 		ASSIGN(kclockinfo);
726 
727 		/* adjust the pointers of the functions and the struct
728 		 * itself to the user-accessible mapping
729 		 */
730 		minix_kerninfo.kerninfo_magic = KERNINFO_MAGIC;
731 		minix_kerninfo.minix_feature_flags = minix_feature_flags;
732 		minix_kerninfo_user = (vir_bytes) FIXEDPTR(&minix_kerninfo);
733 
734 		minix_kerninfo.ki_flags |= MINIX_KIF_USERINFO;
735 
736 		return OK;
737 	}
738 
739 	if (index == usermapped_index) {
740 		return OK;
741 	}
742 
743 	/* if this all fails loop over the maps */
744 	/* list over the maps and index them */
745 	phys_maps = kern_phys_map_head;
746 	while(phys_maps != NULL){
747 		if(phys_maps->index == index){
748 			assert(phys_maps->cb != NULL);
749 			/* only update the vir addr we are
750 			   going to call the callback in enable
751 			   paging
752 			*/
753 			phys_maps->vir = addr;
754 			return OK;
755 		}
756 		phys_maps = phys_maps->next;
757 	}
758 
759 	return EINVAL;
760 }
761 
762 int arch_enable_paging(struct proc * caller)
763 {
764 	kern_phys_map *phys_maps;
765 	assert(caller->p_seg.p_ttbr);
766 
767 
768 	/* load caller's page table */
769 	switch_address_space(caller);
770 
771 	/* We have now switched address spaces and the mappings are
772 	   valid. We can now remap previous mappings. This is not a
773 	   good time to do printf as the initial massing is gone and
774 	   the new mapping is not in place */
775 	phys_maps = kern_phys_map_head;
776 	while(phys_maps != NULL){
777 		assert(phys_maps->cb != NULL);
778 		phys_maps->cb(phys_maps->id, phys_maps->vir);
779 		phys_maps = phys_maps->next;
780 	}
781 
782 	return OK;
783 }
784 
785 void release_address_space(struct proc *pr)
786 {
787 	pr->p_seg.p_ttbr_v = NULL;
788 	barrier();
789 }
790 
791 
792 
793 /*
794  * Request a physical mapping
795  */
796 int kern_req_phys_map( phys_bytes base_address, vir_bytes io_size,
797 		       int vm_flags, kern_phys_map * priv,
798 		       kern_phys_map_mapped cb, vir_bytes id)
799 {
800 	/* Assign the values to the given struct and add priv
801 	to the list */
802 	assert(base_address != 0);
803 	assert(io_size % ARM_PAGE_SIZE == 0);
804 	assert(cb != NULL);
805 
806 	priv->addr  = base_address;
807 	priv->size  = io_size;
808 	priv->vm_flags  = vm_flags;
809 	priv->cb  = cb;
810 	priv->id  = id;
811 	priv->index = -1;
812 	priv->next = NULL;
813 
814 
815 	if (kern_phys_map_head == NULL){
816 		/* keep a list of items this is the first one */
817 		kern_phys_map_head = priv;
818 		kern_phys_map_head->next = NULL;
819 	} else {
820 		/* insert the item head but first keep track
821 		   of the current by putting it in next */
822 		priv->next = kern_phys_map_head;
823 		/* replace the head */
824 		kern_phys_map_head = priv;
825 	}
826 	return 0;
827 }
828 
829 /*
830  * Callback implementation where the id given to the
831  * kern_phys_map is a pointer to the io map base address.
832  * this implementation will just change that base address.
833  * once that area is remapped.
834  */
835 int kern_phys_map_mapped_ptr(vir_bytes id, phys_bytes address){
836 	*((vir_bytes*)id) = address;
837 	return 0;
838 }
839 
840 /*
841  * Request a physical mapping and put the result in the given prt
842  * Note that ptr will only be valid once the callback happened.
843  */
844 int kern_phys_map_ptr(
845 	phys_bytes base_address,
846 	vir_bytes io_size,
847 	int vm_flags,
848 	kern_phys_map * priv,
849 	vir_bytes ptr)
850 {
851 	return kern_req_phys_map(base_address,io_size,vm_flags,priv,kern_phys_map_mapped_ptr,ptr);
852 }
853 
854