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 /* Copy pages. */ 200 PHYS_COPY_CATCH(srcptr, dstptr, chunk, addr); 201 202 if(addr) { 203 /* If addr is nonzero, a page fault was caught. 204 * 205 * phys_copy does all memory accesses word-aligned (rounded 206 * down), so pagefaults can occur at a lower address than 207 * the specified offsets. compute the lower bounds for sanity 208 * check use. 209 */ 210 vir_bytes src_aligned = srcptr & ~0x3, dst_aligned = dstptr & ~0x3; 211 212 if(addr >= src_aligned && addr < (srcptr + chunk)) { 213 return EFAULT_SRC; 214 } 215 if(addr >= dst_aligned && addr < (dstptr + chunk)) { 216 return EFAULT_DST; 217 } 218 219 panic("lin_lin_copy fault out of range"); 220 221 /* Not reached. */ 222 return EFAULT; 223 } 224 225 /* Update counter and addresses for next iteration, if any. */ 226 bytes -= chunk; 227 srclinaddr += chunk; 228 dstlinaddr += chunk; 229 } 230 231 if(srcproc) assert(!RTS_ISSET(srcproc, RTS_SLOT_FREE)); 232 if(dstproc) assert(!RTS_ISSET(dstproc, RTS_SLOT_FREE)); 233 assert(!RTS_ISSET(get_cpulocal_var(ptproc), RTS_SLOT_FREE)); 234 assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v); 235 236 return OK; 237 } 238 239 static u32_t phys_get32(phys_bytes addr) 240 { 241 u32_t v; 242 int r; 243 244 if((r=lin_lin_copy(NULL, addr, 245 proc_addr(SYSTEM), (phys_bytes) &v, sizeof(v))) != OK) { 246 panic("lin_lin_copy for phys_get32 failed: %d", r); 247 } 248 249 return v; 250 } 251 252 /*===========================================================================* 253 * umap_virtual * 254 *===========================================================================*/ 255 phys_bytes umap_virtual(rp, seg, vir_addr, bytes) 256 register struct proc *rp; /* pointer to proc table entry for process */ 257 int seg; /* T, D, or S segment */ 258 vir_bytes vir_addr; /* virtual address in bytes within the seg */ 259 vir_bytes bytes; /* # of bytes to be copied */ 260 { 261 phys_bytes phys = 0; 262 263 if(vm_lookup(rp, vir_addr, &phys, NULL) != OK) { 264 printf("SYSTEM:umap_virtual: vm_lookup of %s: seg 0x%x: 0x%lx failed\n", rp->p_name, seg, vir_addr); 265 phys = 0; 266 } else { 267 if(phys == 0) 268 panic("vm_lookup returned phys: 0x%lx", phys); 269 } 270 271 if(phys == 0) { 272 printf("SYSTEM:umap_virtual: lookup failed\n"); 273 return 0; 274 } 275 276 /* Now make sure addresses are contiguous in physical memory 277 * so that the umap makes sense. 278 */ 279 if(bytes > 0 && vm_lookup_range(rp, vir_addr, NULL, bytes) != bytes) { 280 printf("umap_virtual: %s: %lu at 0x%lx (vir 0x%lx) not contiguous\n", 281 rp->p_name, bytes, vir_addr, vir_addr); 282 return 0; 283 } 284 285 /* phys must be larger than 0 (or the caller will think the call 286 * failed), and address must not cross a page boundary. 287 */ 288 assert(phys); 289 290 return phys; 291 } 292 293 294 /*===========================================================================* 295 * vm_lookup * 296 *===========================================================================*/ 297 int vm_lookup(const struct proc *proc, const vir_bytes virtual, 298 phys_bytes *physical, u32_t *ptent) 299 { 300 u32_t *root, *pt; 301 int pde, pte; 302 u32_t pde_v, pte_v; 303 304 assert(proc); 305 assert(physical); 306 assert(!isemptyp(proc)); 307 assert(HASPT(proc)); 308 309 /* Retrieve page directory entry. */ 310 root = (u32_t *) (proc->p_seg.p_ttbr & ARM_TTBR_ADDR_MASK); 311 assert(!((u32_t) root % ARM_PAGEDIR_SIZE)); 312 pde = ARM_VM_PDE(virtual); 313 assert(pde >= 0 && pde < ARM_VM_DIR_ENTRIES); 314 pde_v = phys_get32((u32_t) (root + pde)); 315 316 if(! ((pde_v & ARM_VM_PDE_PRESENT) 317 || (pde_v & ARM_VM_SECTION_PRESENT) 318 )) { 319 return EFAULT; 320 } 321 322 if(pde_v & ARM_VM_SECTION) { 323 *physical = pde_v & ARM_VM_SECTION_MASK; 324 if(ptent) *ptent = pde_v; 325 *physical += virtual & ARM_VM_OFFSET_MASK_1MB; 326 } else { 327 /* Retrieve page table entry. */ 328 pt = (u32_t *) (pde_v & ARM_VM_PDE_MASK); 329 assert(!((u32_t) pt % ARM_PAGETABLE_SIZE)); 330 pte = ARM_VM_PTE(virtual); 331 assert(pte >= 0 && pte < ARM_VM_PT_ENTRIES); 332 pte_v = phys_get32((u32_t) (pt + pte)); 333 if(!(pte_v & ARM_VM_PTE_PRESENT)) { 334 return EFAULT; 335 } 336 337 if(ptent) *ptent = pte_v; 338 339 /* Actual address now known; retrieve it and add page offset. */ 340 *physical = pte_v & ARM_VM_PTE_MASK; 341 *physical += virtual % ARM_PAGE_SIZE; 342 } 343 344 return OK; 345 } 346 347 /*===========================================================================* 348 * vm_lookup_range * 349 *===========================================================================*/ 350 size_t vm_lookup_range(const struct proc *proc, vir_bytes vir_addr, 351 phys_bytes *phys_addr, size_t bytes) 352 { 353 /* Look up the physical address corresponding to linear virtual address 354 * 'vir_addr' for process 'proc'. Return the size of the range covered 355 * by contiguous physical memory starting from that address; this may 356 * be anywhere between 0 and 'bytes' inclusive. If the return value is 357 * nonzero, and 'phys_addr' is non-NULL, 'phys_addr' will be set to the 358 * base physical address of the range. 'vir_addr' and 'bytes' need not 359 * be page-aligned, but the caller must have verified that the given 360 * linear range is valid for the given process at all. 361 */ 362 phys_bytes phys, next_phys; 363 size_t len; 364 365 assert(proc); 366 assert(bytes > 0); 367 assert(HASPT(proc)); 368 369 /* Look up the first page. */ 370 if (vm_lookup(proc, vir_addr, &phys, NULL) != OK) 371 return 0; 372 373 if (phys_addr != NULL) 374 *phys_addr = phys; 375 376 len = ARM_PAGE_SIZE - (vir_addr % ARM_PAGE_SIZE); 377 vir_addr += len; 378 next_phys = phys + len; 379 380 /* Look up any next pages and test physical contiguity. */ 381 while (len < bytes) { 382 if (vm_lookup(proc, vir_addr, &phys, NULL) != OK) 383 break; 384 385 if (next_phys != phys) 386 break; 387 388 len += ARM_PAGE_SIZE; 389 vir_addr += ARM_PAGE_SIZE; 390 next_phys += ARM_PAGE_SIZE; 391 } 392 393 /* We might now have overshot the requested length somewhat. */ 394 return MIN(bytes, len); 395 } 396 397 /*===========================================================================* 398 * vm_check_range * 399 *===========================================================================*/ 400 int vm_check_range(struct proc *caller, struct proc *target, 401 vir_bytes vir_addr, size_t bytes, int writeflag) 402 { 403 /* Public interface to vm_suspend(), for use by kernel calls. On behalf 404 * of 'caller', call into VM to check linear virtual address range of 405 * process 'target', starting at 'vir_addr', for 'bytes' bytes. This 406 * function assumes that it will called twice if VM returned an error 407 * the first time (since nothing has changed in that case), and will 408 * then return the error code resulting from the first call. Upon the 409 * first call, a non-success error code is returned as well. 410 */ 411 int r; 412 413 if ((caller->p_misc_flags & MF_KCALL_RESUME) && 414 (r = caller->p_vmrequest.vmresult) != OK) 415 return r; 416 417 vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL, 418 writeflag); 419 420 return VMSUSPEND; 421 } 422 423 /*===========================================================================* 424 * vmmemset * 425 *===========================================================================*/ 426 int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c, 427 phys_bytes count) 428 { 429 u32_t pattern; 430 struct proc *whoptr = NULL; 431 phys_bytes cur_ph = ph; 432 phys_bytes left = count; 433 phys_bytes ptr, chunk, pfa = 0; 434 int new_ttbr, r = OK; 435 436 if ((r = check_resumed_caller(caller)) != OK) 437 return r; 438 439 /* NONE for physical, otherwise virtual */ 440 if (who != NONE && !(whoptr = endpoint_lookup(who))) 441 return ESRCH; 442 443 c &= 0xFF; 444 pattern = c | (c << 8) | (c << 16) | (c << 24); 445 446 assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v); 447 assert(!catch_pagefaults); 448 catch_pagefaults = 1; 449 450 /* We can memset as many bytes as we have remaining, 451 * or as many as remain in the 1MB chunk we mapped in. 452 */ 453 while (left > 0) { 454 new_ttbr = 0; 455 chunk = left; 456 ptr = createpde(whoptr, cur_ph, &chunk, 0, &new_ttbr); 457 458 if (new_ttbr) { 459 reload_ttbr0(); 460 } 461 /* If a page fault happens, pfa is non-null */ 462 if ((pfa = phys_memset(ptr, pattern, chunk))) { 463 464 /* If a process pagefaults, VM may help out */ 465 if (whoptr) { 466 vm_suspend(caller, whoptr, ph, count, 467 VMSTYPE_KERNELCALL, 1); 468 assert(catch_pagefaults); 469 catch_pagefaults = 0; 470 return VMSUSPEND; 471 } 472 473 /* Pagefault when phys copying ?! */ 474 panic("vm_memset: pf %lx addr=%lx len=%lu\n", 475 pfa , ptr, chunk); 476 } 477 478 cur_ph += chunk; 479 left -= chunk; 480 } 481 482 assert(get_cpulocal_var(ptproc)->p_seg.p_ttbr_v); 483 assert(catch_pagefaults); 484 catch_pagefaults = 0; 485 486 return OK; 487 } 488 489 /*===========================================================================* 490 * virtual_copy_f * 491 *===========================================================================*/ 492 int virtual_copy_f(caller, src_addr, dst_addr, bytes, vmcheck) 493 struct proc * caller; 494 struct vir_addr *src_addr; /* source virtual address */ 495 struct vir_addr *dst_addr; /* destination virtual address */ 496 vir_bytes bytes; /* # of bytes to copy */ 497 int vmcheck; /* if nonzero, can return VMSUSPEND */ 498 { 499 /* Copy bytes from virtual address src_addr to virtual address dst_addr. */ 500 struct vir_addr *vir_addr[2]; /* virtual source and destination address */ 501 int i, r; 502 struct proc *procs[2]; 503 504 assert((vmcheck && caller) || (!vmcheck && !caller)); 505 506 /* Check copy count. */ 507 if (bytes <= 0) return(EDOM); 508 509 /* Do some more checks and map virtual addresses to physical addresses. */ 510 vir_addr[_SRC_] = src_addr; 511 vir_addr[_DST_] = dst_addr; 512 513 for (i=_SRC_; i<=_DST_; i++) { 514 endpoint_t proc_e = vir_addr[i]->proc_nr_e; 515 int proc_nr; 516 struct proc *p; 517 518 if(proc_e == NONE) { 519 p = NULL; 520 } else { 521 if(!isokendpt(proc_e, &proc_nr)) { 522 printf("virtual_copy: no reasonable endpoint\n"); 523 return ESRCH; 524 } 525 p = proc_addr(proc_nr); 526 } 527 528 procs[i] = p; 529 } 530 531 if ((r = check_resumed_caller(caller)) != OK) 532 return r; 533 534 if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset, 535 procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) { 536 int writeflag; 537 struct proc *target = NULL; 538 phys_bytes lin; 539 if(r != EFAULT_SRC && r != EFAULT_DST) 540 panic("lin_lin_copy failed: %d", r); 541 if(!vmcheck || !caller) { 542 return r; 543 } 544 545 if(r == EFAULT_SRC) { 546 lin = vir_addr[_SRC_]->offset; 547 target = procs[_SRC_]; 548 writeflag = 0; 549 } else if(r == EFAULT_DST) { 550 lin = vir_addr[_DST_]->offset; 551 target = procs[_DST_]; 552 writeflag = 1; 553 } else { 554 panic("r strange: %d", r); 555 } 556 557 assert(caller); 558 assert(target); 559 560 vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag); 561 return VMSUSPEND; 562 } 563 564 return OK; 565 } 566 567 /*===========================================================================* 568 * data_copy * 569 *===========================================================================*/ 570 int data_copy(const endpoint_t from_proc, const vir_bytes from_addr, 571 const endpoint_t to_proc, const vir_bytes to_addr, 572 size_t bytes) 573 { 574 struct vir_addr src, dst; 575 576 src.offset = from_addr; 577 dst.offset = to_addr; 578 src.proc_nr_e = from_proc; 579 dst.proc_nr_e = to_proc; 580 assert(src.proc_nr_e != NONE); 581 assert(dst.proc_nr_e != NONE); 582 583 return virtual_copy(&src, &dst, bytes); 584 } 585 586 /*===========================================================================* 587 * data_copy_vmcheck * 588 *===========================================================================*/ 589 int data_copy_vmcheck(struct proc * caller, 590 const endpoint_t from_proc, const vir_bytes from_addr, 591 const endpoint_t to_proc, const vir_bytes to_addr, 592 size_t bytes) 593 { 594 struct vir_addr src, dst; 595 596 src.offset = from_addr; 597 dst.offset = to_addr; 598 src.proc_nr_e = from_proc; 599 dst.proc_nr_e = to_proc; 600 assert(src.proc_nr_e != NONE); 601 assert(dst.proc_nr_e != NONE); 602 603 return virtual_copy_vmcheck(caller, &src, &dst, bytes); 604 } 605 606 void memory_init(void) 607 { 608 assert(nfreepdes == 0); 609 610 freepdes[nfreepdes++] = kinfo.freepde_start++; 611 freepdes[nfreepdes++] = kinfo.freepde_start++; 612 613 assert(kinfo.freepde_start < ARM_VM_DIR_ENTRIES); 614 assert(nfreepdes == 2); 615 assert(nfreepdes <= MAXFREEPDES); 616 } 617 618 /*===========================================================================* 619 * arch_proc_init * 620 *===========================================================================*/ 621 void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp, 622 const u32_t ps_str, char *name) 623 { 624 arch_proc_reset(pr); 625 strcpy(pr->p_name, name); 626 627 /* set custom state we know */ 628 pr->p_reg.pc = ip; 629 pr->p_reg.sp = sp; 630 pr->p_reg.retreg = ps_str; /* a.k.a r0*/ 631 } 632 633 static int usermapped_glo_index = -1, 634 usermapped_index = -1, first_um_idx = -1; 635 636 637 /* defined in kernel.lds */ 638 extern char usermapped_start, usermapped_end, usermapped_nonglo_start; 639 640 int arch_phys_map(const int index, 641 phys_bytes *addr, 642 phys_bytes *len, 643 int *flags) 644 { 645 static int first = 1; 646 kern_phys_map *phys_maps; 647 648 int freeidx = 0; 649 u32_t glo_len = (u32_t) &usermapped_nonglo_start - 650 (u32_t) &usermapped_start; 651 652 if(first) { 653 memset(&minix_kerninfo, 0, sizeof(minix_kerninfo)); 654 if(glo_len > 0) { 655 usermapped_glo_index = freeidx++; 656 } 657 658 usermapped_index = freeidx++; 659 first_um_idx = usermapped_index; 660 if(usermapped_glo_index != -1) 661 first_um_idx = usermapped_glo_index; 662 first = 0; 663 664 /* list over the maps and index them */ 665 phys_maps = kern_phys_map_head; 666 while(phys_maps != NULL){ 667 phys_maps->index = freeidx++; 668 phys_maps = phys_maps->next; 669 } 670 671 } 672 673 if(index == usermapped_glo_index) { 674 *addr = vir2phys(&usermapped_start); 675 *len = glo_len; 676 *flags = VMMF_USER | VMMF_GLO; 677 return OK; 678 } 679 else if(index == usermapped_index) { 680 *addr = vir2phys(&usermapped_nonglo_start); 681 *len = (u32_t) &usermapped_end - 682 (u32_t) &usermapped_nonglo_start; 683 *flags = VMMF_USER; 684 return OK; 685 } 686 687 /* if this all fails loop over the maps */ 688 phys_maps = kern_phys_map_head; 689 while(phys_maps != NULL){ 690 if(phys_maps->index == index){ 691 *addr = phys_maps->addr; 692 *len = phys_maps->size; 693 *flags = phys_maps->vm_flags; 694 return OK; 695 } 696 phys_maps = phys_maps->next; 697 } 698 699 return EINVAL; 700 } 701 702 int arch_phys_map_reply(const int index, const vir_bytes addr) 703 { 704 kern_phys_map *phys_maps; 705 706 if(index == first_um_idx) { 707 u32_t usermapped_offset; 708 assert(addr > (u32_t) &usermapped_start); 709 usermapped_offset = addr - (u32_t) &usermapped_start; 710 #define FIXEDPTR(ptr) (void *) ((u32_t)ptr + usermapped_offset) 711 #define FIXPTR(ptr) ptr = FIXEDPTR(ptr) 712 #define ASSIGN(minixstruct) minix_kerninfo.minixstruct = FIXEDPTR(&minixstruct) 713 ASSIGN(kinfo); 714 ASSIGN(machine); 715 ASSIGN(kmessages); 716 ASSIGN(loadinfo); 717 ASSIGN(kuserinfo); 718 ASSIGN(arm_frclock); 719 ASSIGN(kclockinfo); 720 721 /* adjust the pointers of the functions and the struct 722 * itself to the user-accessible mapping 723 */ 724 minix_kerninfo.kerninfo_magic = KERNINFO_MAGIC; 725 minix_kerninfo.minix_feature_flags = minix_feature_flags; 726 minix_kerninfo_user = (vir_bytes) FIXEDPTR(&minix_kerninfo); 727 728 minix_kerninfo.ki_flags |= MINIX_KIF_USERINFO; 729 730 return OK; 731 } 732 733 if (index == usermapped_index) { 734 return OK; 735 } 736 737 /* if this all fails loop over the maps */ 738 /* list over the maps and index them */ 739 phys_maps = kern_phys_map_head; 740 while(phys_maps != NULL){ 741 if(phys_maps->index == index){ 742 assert(phys_maps->cb != NULL); 743 /* only update the vir addr we are 744 going to call the callback in enable 745 paging 746 */ 747 phys_maps->vir = addr; 748 return OK; 749 } 750 phys_maps = phys_maps->next; 751 } 752 753 return EINVAL; 754 } 755 756 int arch_enable_paging(struct proc * caller) 757 { 758 kern_phys_map *phys_maps; 759 assert(caller->p_seg.p_ttbr); 760 761 762 /* load caller's page table */ 763 switch_address_space(caller); 764 765 /* We have now switched address spaces and the mappings are 766 valid. We can now remap previous mappings. This is not a 767 good time to do printf as the initial massing is gone and 768 the new mapping is not in place */ 769 phys_maps = kern_phys_map_head; 770 while(phys_maps != NULL){ 771 assert(phys_maps->cb != NULL); 772 phys_maps->cb(phys_maps->id, phys_maps->vir); 773 phys_maps = phys_maps->next; 774 } 775 776 return OK; 777 } 778 779 void release_address_space(struct proc *pr) 780 { 781 pr->p_seg.p_ttbr_v = NULL; 782 barrier(); 783 } 784 785 786 787 /* 788 * Request a physical mapping 789 */ 790 int kern_req_phys_map( phys_bytes base_address, vir_bytes io_size, 791 int vm_flags, kern_phys_map * priv, 792 kern_phys_map_mapped cb, vir_bytes id) 793 { 794 /* Assign the values to the given struct and add priv 795 to the list */ 796 assert(base_address != 0); 797 assert(io_size % ARM_PAGE_SIZE == 0); 798 assert(cb != NULL); 799 800 priv->addr = base_address; 801 priv->size = io_size; 802 priv->vm_flags = vm_flags; 803 priv->cb = cb; 804 priv->id = id; 805 priv->index = -1; 806 priv->next = NULL; 807 808 809 if (kern_phys_map_head == NULL){ 810 /* keep a list of items this is the first one */ 811 kern_phys_map_head = priv; 812 kern_phys_map_head->next = NULL; 813 } else { 814 /* insert the item head but first keep track 815 of the current by putting it in next */ 816 priv->next = kern_phys_map_head; 817 /* replace the head */ 818 kern_phys_map_head = priv; 819 } 820 return 0; 821 } 822 823 /* 824 * Callback implementation where the id given to the 825 * kern_phys_map is a pointer to the io map base address. 826 * this implementation will just change that base address. 827 * once that area is remapped. 828 */ 829 int kern_phys_map_mapped_ptr(vir_bytes id, phys_bytes address){ 830 *((vir_bytes*)id) = address; 831 return 0; 832 } 833 834 /* 835 * Request a physical mapping and put the result in the given prt 836 * Note that ptr will only be valid once the callback happened. 837 */ 838 int kern_phys_map_ptr( 839 phys_bytes base_address, 840 vir_bytes io_size, 841 int vm_flags, 842 kern_phys_map * priv, 843 vir_bytes ptr) 844 { 845 return kern_req_phys_map(base_address,io_size,vm_flags,priv,kern_phys_map_mapped_ptr,ptr); 846 } 847 848