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