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 195d09f72c4SDavid van Moolenbroek /* Check for overflow. */ 196d09f72c4SDavid van Moolenbroek if (srcptr + chunk < srcptr) return EFAULT_SRC; 197d09f72c4SDavid van Moolenbroek if (dstptr + chunk < dstptr) return EFAULT_DST; 198d09f72c4SDavid 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_check_range * 426433d6423SLionel Sambuc *===========================================================================*/ 427433d6423SLionel Sambuc int vm_check_range(struct proc *caller, struct proc *target, 428433d6423SLionel Sambuc vir_bytes vir_addr, size_t bytes, int writeflag) 429433d6423SLionel Sambuc { 430433d6423SLionel Sambuc /* Public interface to vm_suspend(), for use by kernel calls. On behalf 431433d6423SLionel Sambuc * of 'caller', call into VM to check linear virtual address range of 432433d6423SLionel Sambuc * process 'target', starting at 'vir_addr', for 'bytes' bytes. This 433433d6423SLionel Sambuc * function assumes that it will called twice if VM returned an error 434433d6423SLionel Sambuc * the first time (since nothing has changed in that case), and will 435433d6423SLionel Sambuc * then return the error code resulting from the first call. Upon the 436433d6423SLionel Sambuc * first call, a non-success error code is returned as well. 437433d6423SLionel Sambuc */ 438433d6423SLionel Sambuc int r; 439433d6423SLionel Sambuc 440433d6423SLionel Sambuc if ((caller->p_misc_flags & MF_KCALL_RESUME) && 441433d6423SLionel Sambuc (r = caller->p_vmrequest.vmresult) != OK) 442433d6423SLionel Sambuc return r; 443433d6423SLionel Sambuc 444433d6423SLionel Sambuc vm_suspend(caller, target, vir_addr, bytes, VMSTYPE_KERNELCALL, 445433d6423SLionel Sambuc writeflag); 446433d6423SLionel Sambuc 447433d6423SLionel Sambuc return VMSUSPEND; 448433d6423SLionel Sambuc } 449433d6423SLionel Sambuc 450433d6423SLionel Sambuc #if 0 451433d6423SLionel Sambuc static char *flagstr(u32_t e, const int dir) 452433d6423SLionel Sambuc { 453433d6423SLionel Sambuc static char str[80]; 454433d6423SLionel Sambuc strcpy(str, ""); 455433d6423SLionel Sambuc FLAG(I386_VM_PRESENT); 456433d6423SLionel Sambuc FLAG(I386_VM_WRITE); 457433d6423SLionel Sambuc FLAG(I386_VM_USER); 458433d6423SLionel Sambuc FLAG(I386_VM_PWT); 459433d6423SLionel Sambuc FLAG(I386_VM_PCD); 460433d6423SLionel Sambuc FLAG(I386_VM_GLOBAL); 461433d6423SLionel Sambuc if(dir) 462433d6423SLionel Sambuc FLAG(I386_VM_BIGPAGE); /* Page directory entry only */ 463433d6423SLionel Sambuc else 464433d6423SLionel Sambuc FLAG(I386_VM_DIRTY); /* Page table entry only */ 465433d6423SLionel Sambuc return str; 466433d6423SLionel Sambuc } 467433d6423SLionel Sambuc 468433d6423SLionel Sambuc static void vm_pt_print(u32_t *pagetable, const u32_t v) 469433d6423SLionel Sambuc { 470433d6423SLionel Sambuc int pte; 471433d6423SLionel Sambuc int col = 0; 472433d6423SLionel Sambuc 473433d6423SLionel Sambuc assert(!((u32_t) pagetable % I386_PAGE_SIZE)); 474433d6423SLionel Sambuc 475433d6423SLionel Sambuc for(pte = 0; pte < I386_VM_PT_ENTRIES; pte++) { 476433d6423SLionel Sambuc u32_t pte_v, pfa; 477433d6423SLionel Sambuc pte_v = phys_get32((u32_t) (pagetable + pte)); 478433d6423SLionel Sambuc if(!(pte_v & I386_VM_PRESENT)) 479433d6423SLionel Sambuc continue; 480433d6423SLionel Sambuc pfa = I386_VM_PFA(pte_v); 481433d6423SLionel Sambuc printf("%4d:%08lx:%08lx %2s ", 482433d6423SLionel Sambuc pte, v + I386_PAGE_SIZE*pte, pfa, 483433d6423SLionel Sambuc (pte_v & I386_VM_WRITE) ? "rw":"RO"); 484433d6423SLionel Sambuc col++; 485433d6423SLionel Sambuc if(col == 3) { printf("\n"); col = 0; } 486433d6423SLionel Sambuc } 487433d6423SLionel Sambuc if(col > 0) printf("\n"); 488433d6423SLionel Sambuc 489433d6423SLionel Sambuc return; 490433d6423SLionel Sambuc } 491433d6423SLionel Sambuc 492433d6423SLionel Sambuc static void vm_print(u32_t *root) 493433d6423SLionel Sambuc { 494433d6423SLionel Sambuc int pde; 495433d6423SLionel Sambuc 496433d6423SLionel Sambuc assert(!((u32_t) root % I386_PAGE_SIZE)); 497433d6423SLionel Sambuc 498433d6423SLionel Sambuc printf("page table 0x%lx:\n", root); 499433d6423SLionel Sambuc 500433d6423SLionel Sambuc for(pde = 0; pde < I386_VM_DIR_ENTRIES; pde++) { 501433d6423SLionel Sambuc u32_t pde_v; 502433d6423SLionel Sambuc u32_t *pte_a; 503433d6423SLionel Sambuc pde_v = phys_get32((u32_t) (root + pde)); 504433d6423SLionel Sambuc if(!(pde_v & I386_VM_PRESENT)) 505433d6423SLionel Sambuc continue; 506433d6423SLionel Sambuc if(pde_v & I386_VM_BIGPAGE) { 507433d6423SLionel Sambuc printf("%4d: 0x%lx, flags %s\n", 508433d6423SLionel Sambuc pde, I386_VM_PFA(pde_v), flagstr(pde_v, 1)); 509433d6423SLionel Sambuc } else { 510433d6423SLionel Sambuc pte_a = (u32_t *) I386_VM_PFA(pde_v); 511433d6423SLionel Sambuc printf("%4d: pt %08lx %s\n", 512433d6423SLionel Sambuc pde, pte_a, flagstr(pde_v, 1)); 513433d6423SLionel Sambuc vm_pt_print(pte_a, pde * I386_VM_PT_ENTRIES * I386_PAGE_SIZE); 514433d6423SLionel Sambuc printf("\n"); 515433d6423SLionel Sambuc } 516433d6423SLionel Sambuc } 517433d6423SLionel Sambuc 518433d6423SLionel Sambuc 519433d6423SLionel Sambuc return; 520433d6423SLionel Sambuc } 521433d6423SLionel Sambuc #endif 522433d6423SLionel Sambuc 523433d6423SLionel Sambuc /*===========================================================================* 524433d6423SLionel Sambuc * vmmemset * 525433d6423SLionel Sambuc *===========================================================================*/ 526433d6423SLionel Sambuc int vm_memset(struct proc* caller, endpoint_t who, phys_bytes ph, int c, 527433d6423SLionel Sambuc phys_bytes count) 528433d6423SLionel Sambuc { 529433d6423SLionel Sambuc u32_t pattern; 530433d6423SLionel Sambuc struct proc *whoptr = NULL; 531433d6423SLionel Sambuc phys_bytes cur_ph = ph; 532433d6423SLionel Sambuc phys_bytes left = count; 533433d6423SLionel Sambuc phys_bytes ptr, chunk, pfa = 0; 534433d6423SLionel Sambuc int new_cr3, r = OK; 535433d6423SLionel Sambuc 536433d6423SLionel Sambuc if ((r = check_resumed_caller(caller)) != OK) 537433d6423SLionel Sambuc return r; 538433d6423SLionel Sambuc 539433d6423SLionel Sambuc /* NONE for physical, otherwise virtual */ 540433d6423SLionel Sambuc if (who != NONE && !(whoptr = endpoint_lookup(who))) 541433d6423SLionel Sambuc return ESRCH; 542433d6423SLionel Sambuc 543433d6423SLionel Sambuc c &= 0xFF; 544433d6423SLionel Sambuc pattern = c | (c << 8) | (c << 16) | (c << 24); 545433d6423SLionel Sambuc 546433d6423SLionel Sambuc assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v); 547433d6423SLionel Sambuc assert(!catch_pagefaults); 548433d6423SLionel Sambuc catch_pagefaults = 1; 549433d6423SLionel Sambuc 550433d6423SLionel Sambuc /* We can memset as many bytes as we have remaining, 551433d6423SLionel Sambuc * or as many as remain in the 4MB chunk we mapped in. 552433d6423SLionel Sambuc */ 553433d6423SLionel Sambuc while (left > 0) { 554433d6423SLionel Sambuc new_cr3 = 0; 555433d6423SLionel Sambuc chunk = left; 556433d6423SLionel Sambuc ptr = createpde(whoptr, cur_ph, &chunk, 0, &new_cr3); 557433d6423SLionel Sambuc 558433d6423SLionel Sambuc if (new_cr3) 559433d6423SLionel Sambuc reload_cr3(); 560433d6423SLionel Sambuc 561433d6423SLionel Sambuc /* If a page fault happens, pfa is non-null */ 562433d6423SLionel Sambuc if ((pfa = phys_memset(ptr, pattern, chunk))) { 563433d6423SLionel Sambuc 564433d6423SLionel Sambuc /* If a process pagefaults, VM may help out */ 565433d6423SLionel Sambuc if (whoptr) { 566433d6423SLionel Sambuc vm_suspend(caller, whoptr, ph, count, 567433d6423SLionel Sambuc VMSTYPE_KERNELCALL, 1); 568433d6423SLionel Sambuc assert(catch_pagefaults); 569433d6423SLionel Sambuc catch_pagefaults = 0; 570433d6423SLionel Sambuc return VMSUSPEND; 571433d6423SLionel Sambuc } 572433d6423SLionel Sambuc 573433d6423SLionel Sambuc /* Pagefault when phys copying ?! */ 574433d6423SLionel Sambuc panic("vm_memset: pf %lx addr=%lx len=%lu\n", 575433d6423SLionel Sambuc pfa , ptr, chunk); 576433d6423SLionel Sambuc } 577433d6423SLionel Sambuc 578433d6423SLionel Sambuc cur_ph += chunk; 579433d6423SLionel Sambuc left -= chunk; 580433d6423SLionel Sambuc } 581433d6423SLionel Sambuc 582433d6423SLionel Sambuc assert(get_cpulocal_var(ptproc)->p_seg.p_cr3_v); 583433d6423SLionel Sambuc assert(catch_pagefaults); 584433d6423SLionel Sambuc catch_pagefaults = 0; 585433d6423SLionel Sambuc 586433d6423SLionel Sambuc return OK; 587433d6423SLionel Sambuc } 588433d6423SLionel Sambuc 589433d6423SLionel Sambuc /*===========================================================================* 590433d6423SLionel Sambuc * virtual_copy_f * 591433d6423SLionel Sambuc *===========================================================================*/ 592433d6423SLionel Sambuc int virtual_copy_f(caller, src_addr, dst_addr, bytes, vmcheck) 593433d6423SLionel Sambuc struct proc * caller; 594433d6423SLionel Sambuc struct vir_addr *src_addr; /* source virtual address */ 595433d6423SLionel Sambuc struct vir_addr *dst_addr; /* destination virtual address */ 596433d6423SLionel Sambuc vir_bytes bytes; /* # of bytes to copy */ 597433d6423SLionel Sambuc int vmcheck; /* if nonzero, can return VMSUSPEND */ 598433d6423SLionel Sambuc { 599433d6423SLionel Sambuc /* Copy bytes from virtual address src_addr to virtual address dst_addr. */ 600433d6423SLionel Sambuc struct vir_addr *vir_addr[2]; /* virtual source and destination address */ 601433d6423SLionel Sambuc int i, r; 602433d6423SLionel Sambuc struct proc *procs[2]; 603433d6423SLionel Sambuc 604433d6423SLionel Sambuc assert((vmcheck && caller) || (!vmcheck && !caller)); 605433d6423SLionel Sambuc 606433d6423SLionel Sambuc /* Check copy count. */ 607433d6423SLionel Sambuc if (bytes <= 0) return(EDOM); 608433d6423SLionel Sambuc 609433d6423SLionel Sambuc /* Do some more checks and map virtual addresses to physical addresses. */ 610433d6423SLionel Sambuc vir_addr[_SRC_] = src_addr; 611433d6423SLionel Sambuc vir_addr[_DST_] = dst_addr; 612433d6423SLionel Sambuc 613433d6423SLionel Sambuc for (i=_SRC_; i<=_DST_; i++) { 614433d6423SLionel Sambuc endpoint_t proc_e = vir_addr[i]->proc_nr_e; 615433d6423SLionel Sambuc int proc_nr; 616433d6423SLionel Sambuc struct proc *p; 617433d6423SLionel Sambuc 618433d6423SLionel Sambuc if(proc_e == NONE) { 619433d6423SLionel Sambuc p = NULL; 620433d6423SLionel Sambuc } else { 621433d6423SLionel Sambuc if(!isokendpt(proc_e, &proc_nr)) { 622433d6423SLionel Sambuc printf("virtual_copy: no reasonable endpoint\n"); 623433d6423SLionel Sambuc return ESRCH; 624433d6423SLionel Sambuc } 625433d6423SLionel Sambuc p = proc_addr(proc_nr); 626433d6423SLionel Sambuc } 627433d6423SLionel Sambuc 628433d6423SLionel Sambuc procs[i] = p; 629433d6423SLionel Sambuc } 630433d6423SLionel Sambuc 631433d6423SLionel Sambuc if ((r = check_resumed_caller(caller)) != OK) 632433d6423SLionel Sambuc return r; 633433d6423SLionel Sambuc 634433d6423SLionel Sambuc if((r=lin_lin_copy(procs[_SRC_], vir_addr[_SRC_]->offset, 635433d6423SLionel Sambuc procs[_DST_], vir_addr[_DST_]->offset, bytes)) != OK) { 636433d6423SLionel Sambuc int writeflag; 637433d6423SLionel Sambuc struct proc *target = NULL; 638433d6423SLionel Sambuc phys_bytes lin; 639433d6423SLionel Sambuc if(r != EFAULT_SRC && r != EFAULT_DST) 640433d6423SLionel Sambuc panic("lin_lin_copy failed: %d", r); 641433d6423SLionel Sambuc if(!vmcheck || !caller) { 642433d6423SLionel Sambuc return r; 643433d6423SLionel Sambuc } 644433d6423SLionel Sambuc 645433d6423SLionel Sambuc if(r == EFAULT_SRC) { 646433d6423SLionel Sambuc lin = vir_addr[_SRC_]->offset; 647433d6423SLionel Sambuc target = procs[_SRC_]; 648433d6423SLionel Sambuc writeflag = 0; 649433d6423SLionel Sambuc } else if(r == EFAULT_DST) { 650433d6423SLionel Sambuc lin = vir_addr[_DST_]->offset; 651433d6423SLionel Sambuc target = procs[_DST_]; 652433d6423SLionel Sambuc writeflag = 1; 653433d6423SLionel Sambuc } else { 654433d6423SLionel Sambuc panic("r strange: %d", r); 655433d6423SLionel Sambuc } 656433d6423SLionel Sambuc 657433d6423SLionel Sambuc assert(caller); 658433d6423SLionel Sambuc assert(target); 659433d6423SLionel Sambuc 660433d6423SLionel Sambuc vm_suspend(caller, target, lin, bytes, VMSTYPE_KERNELCALL, writeflag); 661433d6423SLionel Sambuc return VMSUSPEND; 662433d6423SLionel Sambuc } 663433d6423SLionel Sambuc 664433d6423SLionel Sambuc return OK; 665433d6423SLionel Sambuc } 666433d6423SLionel Sambuc 667433d6423SLionel Sambuc /*===========================================================================* 668433d6423SLionel Sambuc * data_copy * 669433d6423SLionel Sambuc *===========================================================================*/ 670433d6423SLionel Sambuc int data_copy(const endpoint_t from_proc, const vir_bytes from_addr, 671433d6423SLionel Sambuc const endpoint_t to_proc, const vir_bytes to_addr, 672433d6423SLionel Sambuc size_t bytes) 673433d6423SLionel Sambuc { 674433d6423SLionel Sambuc struct vir_addr src, dst; 675433d6423SLionel Sambuc 676433d6423SLionel Sambuc src.offset = from_addr; 677433d6423SLionel Sambuc dst.offset = to_addr; 678433d6423SLionel Sambuc src.proc_nr_e = from_proc; 679433d6423SLionel Sambuc dst.proc_nr_e = to_proc; 680433d6423SLionel Sambuc assert(src.proc_nr_e != NONE); 681433d6423SLionel Sambuc assert(dst.proc_nr_e != NONE); 682433d6423SLionel Sambuc 683433d6423SLionel Sambuc return virtual_copy(&src, &dst, bytes); 684433d6423SLionel Sambuc } 685433d6423SLionel Sambuc 686433d6423SLionel Sambuc /*===========================================================================* 687433d6423SLionel Sambuc * data_copy_vmcheck * 688433d6423SLionel Sambuc *===========================================================================*/ 689433d6423SLionel Sambuc int data_copy_vmcheck(struct proc * caller, 690433d6423SLionel Sambuc const endpoint_t from_proc, const vir_bytes from_addr, 691433d6423SLionel Sambuc const endpoint_t to_proc, const vir_bytes to_addr, 692433d6423SLionel Sambuc size_t bytes) 693433d6423SLionel Sambuc { 694433d6423SLionel Sambuc struct vir_addr src, dst; 695433d6423SLionel Sambuc 696433d6423SLionel Sambuc src.offset = from_addr; 697433d6423SLionel Sambuc dst.offset = to_addr; 698433d6423SLionel Sambuc src.proc_nr_e = from_proc; 699433d6423SLionel Sambuc dst.proc_nr_e = to_proc; 700433d6423SLionel Sambuc assert(src.proc_nr_e != NONE); 701433d6423SLionel Sambuc assert(dst.proc_nr_e != NONE); 702433d6423SLionel Sambuc 703433d6423SLionel Sambuc return virtual_copy_vmcheck(caller, &src, &dst, bytes); 704433d6423SLionel Sambuc } 705433d6423SLionel Sambuc 706433d6423SLionel Sambuc void memory_init(void) 707433d6423SLionel Sambuc { 708433d6423SLionel Sambuc assert(nfreepdes == 0); 709433d6423SLionel Sambuc 710433d6423SLionel Sambuc freepdes[nfreepdes++] = kinfo.freepde_start++; 711433d6423SLionel Sambuc freepdes[nfreepdes++] = kinfo.freepde_start++; 712433d6423SLionel Sambuc 713433d6423SLionel Sambuc assert(kinfo.freepde_start < I386_VM_DIR_ENTRIES); 714433d6423SLionel Sambuc assert(nfreepdes == 2); 715433d6423SLionel Sambuc assert(nfreepdes <= MAXFREEPDES); 716433d6423SLionel Sambuc } 717433d6423SLionel Sambuc 718433d6423SLionel Sambuc /*===========================================================================* 719433d6423SLionel Sambuc * arch_proc_init * 720433d6423SLionel Sambuc *===========================================================================*/ 721433d6423SLionel Sambuc void arch_proc_init(struct proc *pr, const u32_t ip, const u32_t sp, 722433d6423SLionel Sambuc const u32_t ps_str, char *name) 723433d6423SLionel Sambuc { 724433d6423SLionel Sambuc arch_proc_reset(pr); 725433d6423SLionel Sambuc strlcpy(pr->p_name, name, sizeof(pr->p_name)); 726433d6423SLionel Sambuc 727433d6423SLionel Sambuc /* set custom state we know */ 728433d6423SLionel Sambuc pr->p_reg.pc = ip; 729433d6423SLionel Sambuc pr->p_reg.sp = sp; 730433d6423SLionel Sambuc pr->p_reg.bx = ps_str; 731433d6423SLionel Sambuc } 732433d6423SLionel Sambuc 733433d6423SLionel Sambuc static int oxpcie_mapping_index = -1, 734433d6423SLionel Sambuc lapic_mapping_index = -1, 735433d6423SLionel Sambuc ioapic_first_index = -1, 736433d6423SLionel Sambuc ioapic_last_index = -1, 737433d6423SLionel Sambuc video_mem_mapping_index = -1, 738433d6423SLionel Sambuc usermapped_glo_index = -1, 739433d6423SLionel Sambuc usermapped_index = -1, first_um_idx = -1; 740433d6423SLionel Sambuc 741433d6423SLionel Sambuc extern char *video_mem; 742433d6423SLionel Sambuc 743433d6423SLionel Sambuc extern char usermapped_start, usermapped_end, usermapped_nonglo_start; 744433d6423SLionel Sambuc 745433d6423SLionel Sambuc int arch_phys_map(const int index, 746433d6423SLionel Sambuc phys_bytes *addr, 747433d6423SLionel Sambuc phys_bytes *len, 748433d6423SLionel Sambuc int *flags) 749433d6423SLionel Sambuc { 750433d6423SLionel Sambuc static int first = 1; 751433d6423SLionel Sambuc int freeidx = 0; 752433d6423SLionel Sambuc static char *ser_var = NULL; 753433d6423SLionel Sambuc u32_t glo_len = (u32_t) &usermapped_nonglo_start - 754433d6423SLionel Sambuc (u32_t) &usermapped_start; 755433d6423SLionel Sambuc 756433d6423SLionel Sambuc if(first) { 757433d6423SLionel Sambuc memset(&minix_kerninfo, 0, sizeof(minix_kerninfo)); 758433d6423SLionel Sambuc video_mem_mapping_index = freeidx++; 759433d6423SLionel Sambuc if(glo_len > 0) { 760433d6423SLionel Sambuc usermapped_glo_index = freeidx++; 761433d6423SLionel Sambuc } 762433d6423SLionel Sambuc 763433d6423SLionel Sambuc usermapped_index = freeidx++; 764433d6423SLionel Sambuc first_um_idx = usermapped_index; 765433d6423SLionel Sambuc if(usermapped_glo_index != -1) 766433d6423SLionel Sambuc first_um_idx = usermapped_glo_index; 767433d6423SLionel Sambuc 768433d6423SLionel Sambuc #ifdef USE_APIC 769433d6423SLionel Sambuc if(lapic_addr) 770433d6423SLionel Sambuc lapic_mapping_index = freeidx++; 771433d6423SLionel Sambuc if (ioapic_enabled) { 772433d6423SLionel Sambuc ioapic_first_index = freeidx; 773433d6423SLionel Sambuc assert(nioapics > 0); 774433d6423SLionel Sambuc freeidx += nioapics; 775433d6423SLionel Sambuc ioapic_last_index = freeidx-1; 776433d6423SLionel Sambuc } 777433d6423SLionel Sambuc #endif 778433d6423SLionel Sambuc 779433d6423SLionel Sambuc #ifdef CONFIG_OXPCIE 780433d6423SLionel Sambuc if((ser_var = env_get("oxpcie"))) { 781433d6423SLionel Sambuc if(ser_var[0] != '0' || ser_var[1] != 'x') { 782433d6423SLionel Sambuc printf("oxpcie address in hex please\n"); 783433d6423SLionel Sambuc } else { 784433d6423SLionel Sambuc printf("oxpcie address is %s\n", ser_var); 785433d6423SLionel Sambuc oxpcie_mapping_index = freeidx++; 786433d6423SLionel Sambuc } 787433d6423SLionel Sambuc } 788433d6423SLionel Sambuc #endif 789433d6423SLionel Sambuc 790433d6423SLionel Sambuc first = 0; 791433d6423SLionel Sambuc } 792433d6423SLionel Sambuc 793433d6423SLionel Sambuc if(index == usermapped_glo_index) { 794433d6423SLionel Sambuc *addr = vir2phys(&usermapped_start); 795433d6423SLionel Sambuc *len = glo_len; 796433d6423SLionel Sambuc *flags = VMMF_USER | VMMF_GLO; 797433d6423SLionel Sambuc return OK; 798433d6423SLionel Sambuc } 799433d6423SLionel Sambuc else if(index == usermapped_index) { 800433d6423SLionel Sambuc *addr = vir2phys(&usermapped_nonglo_start); 801433d6423SLionel Sambuc *len = (u32_t) &usermapped_end - 802433d6423SLionel Sambuc (u32_t) &usermapped_nonglo_start; 803433d6423SLionel Sambuc *flags = VMMF_USER; 804433d6423SLionel Sambuc return OK; 805433d6423SLionel Sambuc } 806433d6423SLionel Sambuc else if (index == video_mem_mapping_index) { 807433d6423SLionel Sambuc /* map video memory in so we can print panic messages */ 808433d6423SLionel Sambuc *addr = MULTIBOOT_VIDEO_BUFFER; 809433d6423SLionel Sambuc *len = I386_PAGE_SIZE; 810433d6423SLionel Sambuc *flags = VMMF_WRITE; 811433d6423SLionel Sambuc return OK; 812433d6423SLionel Sambuc } 813433d6423SLionel Sambuc #ifdef USE_APIC 814433d6423SLionel Sambuc else if (index == lapic_mapping_index) { 815433d6423SLionel Sambuc /* map the local APIC if enabled */ 816433d6423SLionel Sambuc if (!lapic_addr) 817433d6423SLionel Sambuc return EINVAL; 818433d6423SLionel Sambuc *addr = lapic_addr; 819433d6423SLionel Sambuc *len = 4 << 10 /* 4kB */; 820433d6423SLionel Sambuc *flags = VMMF_UNCACHED | VMMF_WRITE; 821433d6423SLionel Sambuc return OK; 822433d6423SLionel Sambuc } 823433d6423SLionel Sambuc else if (ioapic_enabled && index >= ioapic_first_index && index <= ioapic_last_index) { 824433d6423SLionel Sambuc int ioapic_idx = index - ioapic_first_index; 825433d6423SLionel Sambuc *addr = io_apic[ioapic_idx].paddr; 826433d6423SLionel Sambuc assert(*addr); 827433d6423SLionel Sambuc *len = 4 << 10 /* 4kB */; 828433d6423SLionel Sambuc *flags = VMMF_UNCACHED | VMMF_WRITE; 829433d6423SLionel Sambuc printf("ioapic map: addr 0x%lx\n", *addr); 830433d6423SLionel Sambuc return OK; 831433d6423SLionel Sambuc } 832433d6423SLionel Sambuc #endif 833433d6423SLionel Sambuc 834433d6423SLionel Sambuc #if CONFIG_OXPCIE 835433d6423SLionel Sambuc if(index == oxpcie_mapping_index) { 836433d6423SLionel Sambuc *addr = strtoul(ser_var+2, NULL, 16); 837433d6423SLionel Sambuc *len = 0x4000; 838433d6423SLionel Sambuc *flags = VMMF_UNCACHED | VMMF_WRITE; 839433d6423SLionel Sambuc return OK; 840433d6423SLionel Sambuc } 841433d6423SLionel Sambuc #endif 842433d6423SLionel Sambuc 843433d6423SLionel Sambuc return EINVAL; 844433d6423SLionel Sambuc } 845433d6423SLionel Sambuc 846433d6423SLionel Sambuc int arch_phys_map_reply(const int index, const vir_bytes addr) 847433d6423SLionel Sambuc { 848433d6423SLionel Sambuc #ifdef USE_APIC 849433d6423SLionel Sambuc /* if local APIC is enabled */ 850433d6423SLionel Sambuc if (index == lapic_mapping_index && lapic_addr) { 851433d6423SLionel Sambuc lapic_addr_vaddr = addr; 852433d6423SLionel Sambuc return OK; 853433d6423SLionel Sambuc } 854433d6423SLionel Sambuc else if (ioapic_enabled && index >= ioapic_first_index && 855433d6423SLionel Sambuc index <= ioapic_last_index) { 856433d6423SLionel Sambuc int i = index - ioapic_first_index; 857433d6423SLionel Sambuc io_apic[i].vaddr = addr; 858433d6423SLionel Sambuc return OK; 859433d6423SLionel Sambuc } 860433d6423SLionel Sambuc #endif 861433d6423SLionel Sambuc 862433d6423SLionel Sambuc #if CONFIG_OXPCIE 863433d6423SLionel Sambuc if (index == oxpcie_mapping_index) { 864433d6423SLionel Sambuc oxpcie_set_vaddr((unsigned char *) addr); 865433d6423SLionel Sambuc return OK; 866433d6423SLionel Sambuc } 867433d6423SLionel Sambuc #endif 868433d6423SLionel Sambuc if(index == first_um_idx) { 869433d6423SLionel Sambuc extern struct minix_ipcvecs minix_ipcvecs_sysenter, 870433d6423SLionel Sambuc minix_ipcvecs_syscall, 871433d6423SLionel Sambuc minix_ipcvecs_softint; 872433d6423SLionel Sambuc extern u32_t usermapped_offset; 873433d6423SLionel Sambuc assert(addr > (u32_t) &usermapped_start); 874433d6423SLionel Sambuc usermapped_offset = addr - (u32_t) &usermapped_start; 875433d6423SLionel Sambuc #define FIXEDPTR(ptr) (void *) ((u32_t)ptr + usermapped_offset) 876433d6423SLionel Sambuc #define FIXPTR(ptr) ptr = FIXEDPTR(ptr) 877433d6423SLionel Sambuc #define ASSIGN(minixstruct) minix_kerninfo.minixstruct = FIXEDPTR(&minixstruct) 878433d6423SLionel Sambuc ASSIGN(kinfo); 879433d6423SLionel Sambuc ASSIGN(machine); 880433d6423SLionel Sambuc ASSIGN(kmessages); 881433d6423SLionel Sambuc ASSIGN(loadinfo); 882*d91f738bSDavid van Moolenbroek ASSIGN(kclockinfo); 883433d6423SLionel Sambuc 884433d6423SLionel Sambuc /* select the right set of IPC routines to map into processes */ 885433d6423SLionel Sambuc if(minix_feature_flags & MKF_I386_INTEL_SYSENTER) { 886da9af514SLionel Sambuc DEBUGBASIC(("kernel: selecting intel sysenter ipc style\n")); 887433d6423SLionel Sambuc minix_kerninfo.minix_ipcvecs = &minix_ipcvecs_sysenter; 888433d6423SLionel Sambuc } else if(minix_feature_flags & MKF_I386_AMD_SYSCALL) { 889da9af514SLionel Sambuc DEBUGBASIC(("kernel: selecting amd syscall ipc style\n")); 890433d6423SLionel Sambuc minix_kerninfo.minix_ipcvecs = &minix_ipcvecs_syscall; 891433d6423SLionel Sambuc } else { 892da9af514SLionel Sambuc DEBUGBASIC(("kernel: selecting fallback (int) ipc style\n")); 893433d6423SLionel Sambuc minix_kerninfo.minix_ipcvecs = &minix_ipcvecs_softint; 894433d6423SLionel Sambuc } 895433d6423SLionel Sambuc 896433d6423SLionel Sambuc /* adjust the pointers of the functions and the struct 897433d6423SLionel Sambuc * itself to the user-accessible mapping 898433d6423SLionel Sambuc */ 899433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs->send); 900433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs->receive); 901433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs->sendrec); 902433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs->senda); 903433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs->sendnb); 904433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs->notify); 905433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs->do_kernel_call); 906433d6423SLionel Sambuc FIXPTR(minix_kerninfo.minix_ipcvecs); 907433d6423SLionel Sambuc 908433d6423SLionel Sambuc minix_kerninfo.kerninfo_magic = KERNINFO_MAGIC; 909433d6423SLionel Sambuc minix_kerninfo.minix_feature_flags = minix_feature_flags; 910433d6423SLionel Sambuc minix_kerninfo_user = (vir_bytes) FIXEDPTR(&minix_kerninfo); 911433d6423SLionel Sambuc 912433d6423SLionel Sambuc /* if libc_ipc is set, disable usermapped ipc functions 913433d6423SLionel Sambuc * and force binaries to use in-libc fallbacks. 914433d6423SLionel Sambuc */ 915433d6423SLionel Sambuc if(env_get("libc_ipc")) { 916433d6423SLionel Sambuc printf("kernel: forcing in-libc fallback ipc style\n"); 917433d6423SLionel Sambuc minix_kerninfo.minix_ipcvecs = NULL; 918433d6423SLionel Sambuc } else { 919433d6423SLionel Sambuc minix_kerninfo.ki_flags |= MINIX_KIF_IPCVECS; 920433d6423SLionel Sambuc } 921433d6423SLionel Sambuc 922433d6423SLionel Sambuc return OK; 923433d6423SLionel Sambuc } 924433d6423SLionel Sambuc 925433d6423SLionel Sambuc if(index == usermapped_index) return OK; 926433d6423SLionel Sambuc 927433d6423SLionel Sambuc if (index == video_mem_mapping_index) { 928433d6423SLionel Sambuc video_mem_vaddr = addr; 929433d6423SLionel Sambuc return OK; 930433d6423SLionel Sambuc } 931433d6423SLionel Sambuc 932433d6423SLionel Sambuc return EINVAL; 933433d6423SLionel Sambuc } 934433d6423SLionel Sambuc 935433d6423SLionel Sambuc int arch_enable_paging(struct proc * caller) 936433d6423SLionel Sambuc { 937433d6423SLionel Sambuc assert(caller->p_seg.p_cr3); 938433d6423SLionel Sambuc 939433d6423SLionel Sambuc /* load caller's page table */ 940433d6423SLionel Sambuc switch_address_space(caller); 941433d6423SLionel Sambuc 942433d6423SLionel Sambuc video_mem = (char *) video_mem_vaddr; 943433d6423SLionel Sambuc 944433d6423SLionel Sambuc #ifdef USE_APIC 945433d6423SLionel Sambuc /* start using the virtual addresses */ 946433d6423SLionel Sambuc 947433d6423SLionel Sambuc /* if local APIC is enabled */ 948433d6423SLionel Sambuc if (lapic_addr) { 949433d6423SLionel Sambuc lapic_addr = lapic_addr_vaddr; 950433d6423SLionel Sambuc lapic_eoi_addr = LAPIC_EOI; 951433d6423SLionel Sambuc } 952433d6423SLionel Sambuc /* if IO apics are enabled */ 953433d6423SLionel Sambuc if (ioapic_enabled) { 954433d6423SLionel Sambuc int i; 955433d6423SLionel Sambuc 956433d6423SLionel Sambuc for (i = 0; i < nioapics; i++) { 957433d6423SLionel Sambuc io_apic[i].addr = io_apic[i].vaddr; 958433d6423SLionel Sambuc } 959433d6423SLionel Sambuc } 960433d6423SLionel Sambuc #if CONFIG_SMP 961433d6423SLionel Sambuc barrier(); 962433d6423SLionel Sambuc 963433d6423SLionel Sambuc wait_for_APs_to_finish_booting(); 964433d6423SLionel Sambuc #endif 965433d6423SLionel Sambuc #endif 966433d6423SLionel Sambuc 967433d6423SLionel Sambuc #ifdef USE_WATCHDOG 968433d6423SLionel Sambuc /* 969433d6423SLionel Sambuc * We make sure that we don't enable the watchdog until paging is turned 970433d6423SLionel Sambuc * on as we might get an NMI while switching and we might still use wrong 971433d6423SLionel Sambuc * lapic address. Bad things would happen. It is unfortunate but such is 972433d6423SLionel Sambuc * life 973433d6423SLionel Sambuc */ 974433d6423SLionel Sambuc if (watchdog_enabled) 975433d6423SLionel Sambuc i386_watchdog_start(); 976433d6423SLionel Sambuc #endif 977433d6423SLionel Sambuc 978433d6423SLionel Sambuc return OK; 979433d6423SLionel Sambuc } 980433d6423SLionel Sambuc 981433d6423SLionel Sambuc void release_address_space(struct proc *pr) 982433d6423SLionel Sambuc { 983433d6423SLionel Sambuc pr->p_seg.p_cr3_v = NULL; 984433d6423SLionel Sambuc } 985433d6423SLionel Sambuc 986433d6423SLionel Sambuc /* computes a checksum of a buffer of a given length. The byte sum must be zero */ 987433d6423SLionel Sambuc int platform_tbl_checksum_ok(void *ptr, unsigned int length) 988433d6423SLionel Sambuc { 989433d6423SLionel Sambuc u8_t total = 0; 990433d6423SLionel Sambuc unsigned int i; 991433d6423SLionel Sambuc for (i = 0; i < length; i++) 992433d6423SLionel Sambuc total += ((unsigned char *)ptr)[i]; 993433d6423SLionel Sambuc return !total; 994433d6423SLionel Sambuc } 995433d6423SLionel Sambuc 996433d6423SLionel Sambuc int platform_tbl_ptr(phys_bytes start, 997433d6423SLionel Sambuc phys_bytes end, 998433d6423SLionel Sambuc unsigned increment, 999433d6423SLionel Sambuc void * buff, 1000433d6423SLionel Sambuc unsigned size, 1001433d6423SLionel Sambuc phys_bytes * phys_addr, 1002433d6423SLionel Sambuc int ((* cmp_f)(void *))) 1003433d6423SLionel Sambuc { 1004433d6423SLionel Sambuc phys_bytes addr; 1005433d6423SLionel Sambuc 1006433d6423SLionel Sambuc for (addr = start; addr < end; addr += increment) { 1007433d6423SLionel Sambuc phys_copy (addr, (phys_bytes) buff, size); 1008433d6423SLionel Sambuc if (cmp_f(buff)) { 1009433d6423SLionel Sambuc if (phys_addr) 1010433d6423SLionel Sambuc *phys_addr = addr; 1011433d6423SLionel Sambuc return 1; 1012433d6423SLionel Sambuc } 1013433d6423SLionel Sambuc } 1014433d6423SLionel Sambuc return 0; 1015433d6423SLionel Sambuc } 1016