// Load a program segment into pagetable at virtual address va.// va must be page-aligned// and the pages from va to va+sz must already be mapped.// Returns 0 on success, -1 on failure.staticintloadseg(pagetable_tpagetable, uint64 va,struct inode *ip,uintoffset,uintsz){uint i, n; uint64 pa;for(i =0; i < sz; i += PGSIZE){ pa =walkaddr(pagetable, va + i);if(pa ==0)panic("loadseg: address should exist");if(sz - i < PGSIZE) n = sz - i;else n = PGSIZE;if(readi(ip,0,(uint64)pa, offset+i, n)!= n)return-1;}return0;}
// Allocate some pages at the next page boundary.
// Make the first inaccessible as a stack guard.
// Use the rest as the user stack.
sz = PGROUNDUP(sz);
uint64 sz1;
if((sz1 = uvmalloc(pagetable, sz, sz + (USERSTACK+1)*PGSIZE, PTE_W)) == 0)
goto bad;
sz = sz1;
uvmclear(pagetable, sz-(USERSTACK+1)*PGSIZE);
sp = sz;
stackbase = sp - USERSTACK*PGSIZE;