Xv6 with picoc & Linkage editor
v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
|
00001 #include "param.h" 00002 #include "types.h" 00003 #include "defs.h" 00004 #include "x86.h" 00005 #include "memlayout.h" 00006 #include "mmu.h" 00007 #include "proc.h" 00008 #include "elf.h" 00009 extern char data[]; // defined by kernel.ld 00010 pde_t *kpgdir; // for use in scheduler() 00011 struct segdesc gdt[NSEGS]; 00012 00013 // Set up CPU's kernel segment descriptors. 00014 // Run once on entry on each CPU. 00015 void 00016 seginit(void) 00017 { 00018 struct cpu *c; 00019 00020 // Map "logical" addresses to virtual addresses using identity map. 00021 // Cannot share a CODE descriptor for both kernel and user 00022 // because it would have to have DPL_USR, but the CPU forbids 00023 // an interrupt from CPL=0 to DPL=3. 00024 c = &cpus[cpunum()]; 00025 c->gdt[SEG_KCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, 0); 00026 c->gdt[SEG_KDATA] = SEG(STA_W, 0, 0xffffffff, 0); 00027 c->gdt[SEG_UCODE] = SEG(STA_X|STA_R, 0, 0xffffffff, DPL_USER); 00028 c->gdt[SEG_UDATA] = SEG(STA_W, 0, 0xffffffff, DPL_USER); 00029 00030 // Map cpu, and curproc 00031 c->gdt[SEG_KCPU] = SEG(STA_W, &c->cpu, 8, 0); 00032 00033 lgdt(c->gdt, sizeof(c->gdt)); 00034 loadgs(SEG_KCPU << 3); 00035 00036 // Initialize cpu-local storage. 00037 cpu = c; 00038 proc = 0; 00039 } 00040 00041 // Return the address of the PTE in page table pgdir 00042 // that corresponds to virtual address va. If alloc!=0, 00043 // create any required page table pages. 00044 static pte_t * 00045 walkpgdir(pde_t *pgdir, const void *va, int alloc) 00046 { 00047 pde_t *pde; 00048 pte_t *pgtab; 00049 00050 pde = &pgdir[PDX(va)]; 00051 if(*pde & PTE_P){ 00052 pgtab = (pte_t*)p2v(PTE_ADDR(*pde)); 00053 } else { 00054 if(!alloc || (pgtab = (pte_t*)kalloc()) == 0) 00055 return 0; 00056 // Make sure all those PTE_P bits are zero. 00057 memset(pgtab, 0, PGSIZE); 00058 // The permissions here are overly generous, but they can 00059 // be further restricted by the permissions in the page table 00060 // entries, if necessary. 00061 *pde = v2p(pgtab) | PTE_P | PTE_W | PTE_U; 00062 } 00063 return &pgtab[PTX(va)]; 00064 } 00065 00066 // Create PTEs for virtual addresses starting at va that refer to 00067 // physical addresses starting at pa. va and size might not 00068 // be page-aligned. 00069 static int 00070 mappages(pde_t *pgdir, void *va, uint size, uint pa, int perm) 00071 { 00072 char *a, *last; 00073 pte_t *pte; 00074 00075 a = (char*)PGROUNDDOWN((uint)va); 00076 last = (char*)PGROUNDDOWN(((uint)va) + size - 1); 00077 for(;;){ 00078 if((pte = walkpgdir(pgdir, a, 1)) == 0) 00079 return -1; 00080 if(*pte & PTE_P) 00081 panic("remap"); 00082 *pte = pa | perm | PTE_P; 00083 if(a == last) 00084 break; 00085 a += PGSIZE; 00086 pa += PGSIZE; 00087 } 00088 return 0; 00089 } 00090 00091 // There is one page table per process, plus one that's used when 00092 // a CPU is not running any process (kpgdir). The kernel uses the 00093 // current process's page table during system calls and interrupts; 00094 // page protection bits prevent user code from using the kernel's 00095 // mappings. 00096 // 00097 // setupkvm() and exec() set up every page table like this: 00098 // 00099 // 0..KERNBASE: user memory (text+data+stack+heap), mapped to 00100 // phys memory allocated by the kernel 00101 // KERNBASE..KERNBASE+EXTMEM: mapped to 0..EXTMEM (for I/O space) 00102 // KERNBASE+EXTMEM..data: mapped to EXTMEM..V2P(data) 00103 // for the kernel's instructions and r/o data 00104 // data..KERNBASE+PHYSTOP: mapped to V2P(data)..PHYSTOP, 00105 // rw data + free physical memory 00106 // 0xfe000000..0: mapped direct (devices such as ioapic) 00107 // 00108 // The kernel allocates physical memory for its heap and for user memory 00109 // between V2P(end) and the end of physical memory (PHYSTOP) 00110 // (directly addressable from end..P2V(PHYSTOP)). 00111 00112 // This table defines the kernel's mappings, which are present in 00113 // every process's page table. 00114 static struct kmap { 00115 void *virt; 00116 uint phys_start; 00117 uint phys_end; 00118 int perm; 00119 } kmap[] = { 00120 { (void*) KERNBASE, 0, EXTMEM, PTE_W}, // I/O space 00121 { (void*) KERNLINK, V2P(KERNLINK), V2P(data), 0}, // kernel text+rodata 00122 { (void*) data, V2P(data), PHYSTOP, PTE_W}, // kernel data, memory 00123 { (void*) DEVSPACE, DEVSPACE, 0, PTE_W}, // more devices 00124 }; 00125 00126 // Set up kernel part of a page table. 00127 pde_t* 00128 setupkvm() 00129 { 00130 pde_t *pgdir; 00131 struct kmap *k; 00132 00133 if((pgdir = (pde_t*)kalloc()) == 0) 00134 return 0; 00135 memset(pgdir, 0, PGSIZE); 00136 if (p2v(PHYSTOP) > (void*)DEVSPACE) 00137 panic("PHYSTOP too high"); 00138 for(k = kmap; k < &kmap[NELEM(kmap)]; k++) 00139 if(mappages(pgdir, k->virt, k->phys_end - k->phys_start, 00140 (uint)k->phys_start, k->perm) < 0) 00141 return 0; 00142 return pgdir; 00143 } 00144 00145 // Allocate one page table for the machine for the kernel address 00146 // space for scheduler processes. 00147 void 00148 kvmalloc(void) 00149 { 00150 kpgdir = setupkvm(); 00151 switchkvm(); 00152 } 00153 00154 // Switch h/w page table register to the kernel-only page table, 00155 // for when no process is running. 00156 void 00157 switchkvm(void) 00158 { 00159 lcr3(v2p(kpgdir)); // switch to the kernel page table 00160 } 00161 00162 // Switch TSS and h/w page table to correspond to process p. 00163 void 00164 switchuvm(struct proc *p) 00165 { 00166 pushcli(); 00167 cpu->gdt[SEG_TSS] = SEG16(STS_T32A, &cpu->ts, sizeof(cpu->ts)-1, 0); 00168 cpu->gdt[SEG_TSS].s = 0; 00169 cpu->ts.ss0 = SEG_KDATA << 3; 00170 cpu->ts.esp0 = (uint)proc->kstack + KSTACKSIZE; 00171 ltr(SEG_TSS << 3); 00172 if(p->pgdir == 0) 00173 panic("switchuvm: no pgdir"); 00174 lcr3(v2p(p->pgdir)); // switch to new address space 00175 popcli(); 00176 } 00177 00178 // Load the initcode into address 0 of pgdir. 00179 // sz must be less than a page. 00180 void 00181 inituvm(pde_t *pgdir, char *init, uint sz) 00182 { 00183 char *mem; 00184 00185 if(sz >= PGSIZE) 00186 panic("inituvm: more than a page"); 00187 mem = kalloc(); 00188 memset(mem, 0, PGSIZE); 00189 mappages(pgdir, 0, PGSIZE, v2p(mem), PTE_W|PTE_U); 00190 memmove(mem, init, sz); 00191 } 00192 00193 // Load a program segment into pgdir. addr must be page-aligned 00194 // and the pages from addr to addr+sz must already be mapped. 00195 int 00196 loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz) 00197 { 00198 uint i, pa, n; 00199 pte_t *pte; 00200 00201 if((uint) addr % PGSIZE != 0) 00202 panic("loaduvm: addr must be page aligned"); 00203 for(i = 0; i < sz; i += PGSIZE){ 00204 if((pte = walkpgdir(pgdir, addr+i, 0)) == 0) 00205 panic("loaduvm: address should exist"); 00206 pa = PTE_ADDR(*pte); 00207 if(sz - i < PGSIZE) 00208 n = sz - i; 00209 else 00210 n = PGSIZE; 00211 if(readi(ip, p2v(pa), offset+i, n) != n) 00212 return -1; 00213 } 00214 return 0; 00215 } 00216 00217 // Allocate page tables and physical memory to grow process from oldsz to 00218 // newsz, which need not be page aligned. Returns new size or 0 on error. 00219 int 00220 allocuvm(pde_t *pgdir, uint oldsz, uint newsz) 00221 { 00222 char *mem; 00223 uint a; 00224 00225 if(newsz >= KERNBASE) 00226 return 0; 00227 if(newsz < oldsz) 00228 return oldsz; 00229 00230 a = PGROUNDUP(oldsz); 00231 for(; a < newsz; a += PGSIZE){ 00232 mem = kalloc(); 00233 if(mem == 0){ 00234 cprintf("allocuvm out of memory\n"); 00235 deallocuvm(pgdir, newsz, oldsz); 00236 return 0; 00237 } 00238 memset(mem, 0, PGSIZE); 00239 mappages(pgdir, (char*)a, PGSIZE, v2p(mem), PTE_W|PTE_U); 00240 } 00241 return newsz; 00242 } 00243 00244 00245 00246 // Deallocate user pages to bring the process size from oldsz to 00247 // newsz. oldsz and newsz need not be page-aligned, nor does newsz 00248 // need to be less than oldsz. oldsz can be larger than the actual 00249 // process size. Returns the new process size. 00250 int 00251 deallocuvm(pde_t *pgdir, uint oldsz, uint newsz) 00252 { 00253 pte_t *pte; 00254 uint a, pa; 00255 00256 if(newsz >= oldsz) 00257 return oldsz; 00258 00259 a = PGROUNDUP(newsz); 00260 for(; a < oldsz; a += PGSIZE){ 00261 pte = walkpgdir(pgdir, (char*)a, 0); 00262 if(!pte) 00263 a += (NPTENTRIES - 1) * PGSIZE; 00264 else if((*pte & PTE_P) != 0){ 00265 pa = PTE_ADDR(*pte); 00266 if(pa == 0) 00267 panic("kfree"); 00268 char *v = p2v(pa); 00269 kfree(v); 00270 *pte = 0; 00271 } 00272 } 00273 return newsz; 00274 } 00275 00276 // Free a page table and all the physical memory pages 00277 // in the user part. 00278 void 00279 freevm(pde_t *pgdir) 00280 { 00281 uint i; 00282 00283 if(pgdir == 0) 00284 panic("freevm: no pgdir"); 00285 deallocuvm(pgdir, KERNBASE, 0); 00286 for(i = 0; i < NPDENTRIES; i++){ 00287 if(pgdir[i] & PTE_P){ 00288 char * v = p2v(PTE_ADDR(pgdir[i])); 00289 kfree(v); 00290 } 00291 } 00292 kfree((char*)pgdir); 00293 } 00294 00295 // Clear PTE_U on a page. Used to create an inaccessible 00296 // page beneath the user stack. 00297 void 00298 clearpteu(pde_t *pgdir, char *uva) 00299 { 00300 pte_t *pte; 00301 00302 pte = walkpgdir(pgdir, uva, 0); 00303 if(pte == 0) 00304 panic("clearpteu"); 00305 *pte &= ~PTE_U; 00306 } 00307 00308 // Given a parent process's page table, create a copy 00309 // of it for a child. 00310 pde_t* 00311 copyuvm(pde_t *pgdir, uint sz) 00312 { 00313 pde_t *d; 00314 pte_t *pte; 00315 uint pa, i; 00316 char *mem; 00317 00318 if((d = setupkvm()) == 0) 00319 return 0; 00320 for(i = 0; i < sz; i += PGSIZE){ 00321 if((pte = walkpgdir(pgdir, (void *) i, 0)) == 0) 00322 panic("copyuvm: pte should exist"); 00323 if(!(*pte & PTE_P)) 00324 panic("copyuvm: page not present"); 00325 pa = PTE_ADDR(*pte); 00326 if((mem = kalloc()) == 0) 00327 goto bad; 00328 memmove(mem, (char*)p2v(pa), PGSIZE); 00329 if(mappages(d, (void*)i, PGSIZE, v2p(mem), PTE_W|PTE_U) < 0) 00330 goto bad; 00331 } 00332 return d; 00333 00334 bad: 00335 freevm(d); 00336 return 0; 00337 } 00338 00339 //PAGEBREAK! 00340 // Map user virtual address to kernel address. 00341 char* 00342 uva2ka(pde_t *pgdir, char *uva) 00343 { 00344 pte_t *pte; 00345 00346 pte = walkpgdir(pgdir, uva, 0); 00347 if((*pte & PTE_P) == 0) 00348 return 0; 00349 if((*pte & PTE_U) == 0) 00350 return 0; 00351 return (char*)p2v(PTE_ADDR(*pte)); 00352 } 00353 00354 // Copy len bytes from p to user address va in page table pgdir. 00355 // Most useful when pgdir is not the current page table. 00356 // uva2ka ensures this only works for PTE_U pages. 00357 int 00358 copyout(pde_t *pgdir, uint va, void *p, uint len) 00359 { 00360 char *buf, *pa0; 00361 uint n, va0; 00362 00363 buf = (char*)p; 00364 while(len > 0){ 00365 va0 = (uint)PGROUNDDOWN(va); 00366 pa0 = uva2ka(pgdir, (char*)va0); 00367 if(pa0 == 0) 00368 return -1; 00369 n = PGSIZE - (va - va0); 00370 if(n > len) 00371 n = len; 00372 memmove(pa0 + (va - va0), buf, n); 00373 len -= n; 00374 buf += n; 00375 va = va0 + PGSIZE; 00376 } 00377 return 0; 00378 }