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