Remove building with NOCRYPTO option
[minix3.git] / minix / kernel / arch / earm / memory.c
blobd56d42a691b8df00d629702153f86bd2a176e345
2 #include "kernel/kernel.h"
3 #include "kernel/proc.h"
4 #include "kernel/vm.h"
6 #include <machine/vm.h>
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>
17 #include <machine/vm.h>
19 #include "arch_proto.h"
20 #include "kernel/proto.h"
21 #include "kernel/debug.h"
22 #include "bsp_timer.h"
25 #define HASPT(procptr) ((procptr)->p_seg.p_ttbr != 0)
26 static int nfreepdes = 0;
27 #define MAXFREEPDES 2
28 static int freepdes[MAXFREEPDES];
30 static u32_t phys_get32(phys_bytes v);
32 /* list of requested physical mapping */
33 static kern_phys_map *kern_phys_map_head;
35 void mem_clear_mapcache(void)
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;
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.
55 * It recognizes pr already being in memory as a special case (no
56 * mapping required).
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.
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.
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. */
77 u32_t pdeval;
78 phys_bytes offset;
79 int pde;
81 assert(free_pde_idx >= 0 && free_pde_idx < nfreepdes);
82 pde = freepdes[free_pde_idx];
83 assert(pde >= 0 && pde < 4096);
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.
92 return linaddr;
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.
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);
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;
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.
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;
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.
129 offset = linaddr & ARM_VM_OFFSET_MASK_1MB; /* Offset in 1MB window. */
130 *bytes = MIN(*bytes, ARM_SECTION_SIZE - offset);
132 /* Return the linear address of the start of the new mapping. */
133 return ARM_SECTION_SIZE*pde + offset;
137 /*===========================================================================*
138 * check_resumed_caller *
139 *===========================================================================*/
140 static int check_resumed_caller(struct proc *caller)
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;
148 return OK;
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)
157 u32_t addr;
158 proc_nr_t procslot;
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);
164 procslot = get_cpulocal_var(ptproc)->p_nr;
166 assert(procslot >= 0 && procslot < ARM_VM_DIR_ENTRIES);
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));
175 while(bytes > 0) {
176 phys_bytes srcptr, dstptr;
177 vir_bytes chunk = bytes;
178 int changed = 0;
180 #ifdef CONFIG_SMP
181 unsigned cpu = cpuid;
183 if (srcproc && GET_BIT(srcproc->p_stale_tlb, cpu)) {
184 changed = 1;
185 UNSET_BIT(srcproc->p_stale_tlb, cpu);
187 if (dstproc && GET_BIT(dstproc->p_stale_tlb, cpu)) {
188 changed = 1;
189 UNSET_BIT(dstproc->p_stale_tlb, cpu);
191 #endif
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();
199 /* Check for overflow. */
200 if (srcptr + chunk < srcptr) return EFAULT_SRC;
201 if (dstptr + chunk < dstptr) return EFAULT_DST;
203 /* Copy pages. */
204 PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr);
206 if(addr) {
207 /* If addr is nonzero, a page fault was caught.
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.
214 vir_bytes src_aligned = srcptr & ~0x3, dst_aligned = dstptr & ~0x3;
216 if(addr >= src_aligned && addr < (srcptr + chunk)) {
217 return EFAULT_SRC;
219 if(addr >= dst_aligned && addr < (dstptr + chunk)) {
220 return EFAULT_DST;
223 panic("lin_lin_copy fault out of range");
225 /* Not reached. */
226 return EFAULT;
229 /* Update counter and addresses for next iteration, if any. */
230 bytes -= chunk;
231 srclinaddr += chunk;
232 dstlinaddr += chunk;
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);
240 return OK;
243 static u32_t phys_get32(phys_bytes addr)
245 u32_t v;
246 int r;
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);
253 return v;
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 */
266 phys_bytes phys = 0;
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);
276 if(phys == 0) {
277 printf("SYSTEM:umap_virtual: lookup failed\n");
278 return 0;
281 /* Now make sure addresses are contiguous in physical memory
282 * so that the umap makes sense.
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;
290 /* phys must be larger than 0 (or the caller will think the call
291 * failed), and address must not cross a page boundary.
293 assert(phys);
295 return phys;
299 /*===========================================================================*
300 * vm_lookup *
301 *===========================================================================*/
302 int vm_lookup(const struct proc *proc, const vir_bytes virtual,
303 phys_bytes *physical, u32_t *ptent)
305 u32_t *root, *pt;
306 int pde, pte;
307 u32_t pde_v, pte_v;
309 assert(proc);
310 assert(physical);
311 assert(!isemptyp(proc));
312 assert(HASPT(proc));
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));
321 if(! ((pde_v & ARM_VM_PDE_PRESENT)
322 || (pde_v & ARM_VM_SECTION_PRESENT)
323 )) {
324 return EFAULT;
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;
342 if(ptent) *ptent = pte_v;
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;
349 return OK;
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)
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.
367 phys_bytes phys, next_phys;
368 size_t len;
370 assert(proc);
371 assert(bytes > 0);
372 assert(HASPT(proc));
374 /* Look up the first page. */
375 if (vm_lookup(proc, vir_addr, &phys, NULL) != OK)
376 return 0;
378 if (phys_addr != NULL)
379 *phys_addr = phys;
381 len = ARM_PAGE_SIZE - (vir_addr % ARM_PAGE_SIZE);
382 vir_addr += len;
383 next_phys = phys + len;
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;
390 if (next_phys != phys)
391 break;
393 len += ARM_PAGE_SIZE;
394 vir_addr += ARM_PAGE_SIZE;
395 next_phys += ARM_PAGE_SIZE;
398 /* We might now have overshot the requested length somewhat. */
399 return MIN(bytes, len);
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)
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.
416 int r;
418 if ((caller->p_misc_flags & MF_KCALL_RESUME) &&
419 (r = caller->p_vmrequest.vmresult) != OK)
420 return r;
422 vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL,
423 writeflag);
425 return VMSUSPEND;
428 /*===========================================================================*
429 * vmmemset *
430 *===========================================================================*/
431 int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c,
432 phys_bytes count)
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;
441 if ((r = check_resumed_caller(caller)) != OK)
442 return r;
444 /* NONE for physical, otherwise virtual */
445 if (who != NONE && !(whoptr = endpoint_lookup(who)))
446 return ESRCH;
448 c &= 0xFF;
449 pattern = c | (c << 8) | (c << 16) | (c << 24);
451 assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
452 assert(!catch_pagefaults);
453 catch_pagefaults = 1;
455 /* We can memset as many bytes as we have remaining,
456 * or as many as remain in the 1MB chunk we mapped in.
458 while (left > 0) {
459 new_ttbr = 0;
460 chunk = left;
461 ptr = createpde(whoptr, cur_ph, &chunk, 0, &new_ttbr);
463 if (new_ttbr) {
464 reload_ttbr0();
466 /* If a page fault happens, pfa is non-null */
467 if ((pfa = phys_memset(ptr, pattern, chunk))) {
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;
478 /* Pagefault when phys copying ?! */
479 panic("vm_memset: pf %lx addr=%lx len=%lu\n",
480 pfa , ptr, chunk);
483 cur_ph += chunk;
484 left -= chunk;
487 assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v);
488 assert(catch_pagefaults);
489 catch_pagefaults = 0;
491 return OK;
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 */
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];
510 assert((vmcheck && caller) || (!vmcheck && !caller));
512 /* Check copy count. */
513 if (bytes <= 0) return(EDOM);
515 /* Do some more checks and map virtual addresses to physical addresses. */
516 vir_addr[_SRC_] = src_addr;
517 vir_addr[_DST_] = dst_addr;
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;
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;
531 p = proc_addr(proc_nr);
534 procs[i] = p;
537 if ((r = check_resumed_caller(caller)) != OK)
538 return r;
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;
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);
563 assert(caller);
564 assert(target);
566 vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag);
567 return VMSUSPEND;
570 return OK;
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)
580 struct vir_addr src, dst;
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);
589 return virtual_copy(&src, &dst, bytes);
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)
600 struct vir_addr src, dst;
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);
609 return virtual_copy_vmcheck(caller, &src, &dst, bytes);
612 void memory_init(void)
614 assert(nfreepdes == 0);
616 freepdes[nfreepdes++] = kinfo.freepde_start++;
617 freepdes[nfreepdes++] = kinfo.freepde_start++;
619 assert(kinfo.freepde_start < ARM_VM_DIR_ENTRIES);
620 assert(nfreepdes == 2);
621 assert(nfreepdes <= MAXFREEPDES);
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)
630 arch_proc_reset(pr);
631 strcpy(pr->p_name, name);
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*/
639 static int usermapped_glo_index = -1,
640 usermapped_index = -1, first_um_idx = -1;
643 /* defined in kernel.lds */
644 extern char usermapped_start, usermapped_end, usermapped_nonglo_start;
646 int arch_phys_map(const int index,
647 phys_bytes *addr,
648 phys_bytes *len,
649 int *flags)
651 static int first = 1;
652 kern_phys_map *phys_maps;
654 int freeidx = 0;
655 u32_t glo_len = (u32_t) &usermapped_nonglo_start -
656 (u32_t) &usermapped_start;
658 if(first) {
659 memset(&minix_kerninfo, 0, sizeof(minix_kerninfo));
660 if(glo_len > 0) {
661 usermapped_glo_index = freeidx++;
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;
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;
679 if(index == usermapped_glo_index) {
680 *addr = vir2phys(&usermapped_start);
681 *len = glo_len;
682 *flags = VMMF_USER | VMMF_GLO;
683 return OK;
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;
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;
702 phys_maps = phys_maps->next;
705 return EINVAL;
708 int arch_phys_map_reply(const int index, const vir_bytes addr)
710 kern_phys_map *phys_maps;
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);
727 /* adjust the pointers of the functions and the struct
728 * itself to the user-accessible mapping
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);
734 minix_kerninfo.ki_flags |= MINIX_KIF_USERINFO;
736 return OK;
739 if (index == usermapped_index) {
740 return OK;
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
753 phys_maps->vir = addr;
754 return OK;
756 phys_maps = phys_maps->next;
759 return EINVAL;
762 int arch_enable_paging(struct proc * caller)
764 kern_phys_map *phys_maps;
765 assert(caller->p_seg.p_ttbr);
768 /* load caller's page table */
769 switch_address_space(caller);
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;
782 return OK;
785 void release_address_space(struct proc *pr)
787 pr->p_seg.p_ttbr_v = NULL;
788 barrier();
794 * Request a physical mapping
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)
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);
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;
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;
826 return 0;
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.
835 int kern_phys_map_mapped_ptr(vir_bytes id, phys_bytes address){
836 *((vir_bytes*)id) = address;
837 return 0;
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.
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)
851 return kern_req_phys_map(base_address,io_size,vm_flags,priv,kern_phys_map_mapped_ptr,ptr);