13 /* We load ELF binaries. The following definitions are taken
14 from the ELF specification more-or-less verbatim. */
17 typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
18 typedef uint16_t Elf32_Half;
26 This appears at the very beginning of an ELF binary. */
29 unsigned char e_ident[16];
38 Elf32_Half e_phentsize;
40 Elf32_Half e_shentsize;
42 Elf32_Half e_shstrndx;
46 There are e_phnum of these, starting at file offset e_phoff. */
59 /* Values for p_type. */
60 #define PT_NULL 0 /* Ignore. */
61 #define PT_LOAD 1 /* Loadable segment. */
62 #define PT_DYNAMIC 2 /* Dynamic linking info. */
63 #define PT_INTERP 3 /* Name of dynamic loader. */
64 #define PT_NOTE 4 /* Auxiliary info. */
65 #define PT_SHLIB 5 /* Reserved. */
66 #define PT_PHDR 6 /* Program header table. */
67 #define PT_STACK 0x6474e551 /* Stack segment. */
69 /* Flags for p_flags. */
70 #define PF_X 1 /* Executable. */
71 #define PF_W 2 /* Writable. */
72 #define PF_R 4 /* Readable. */
74 #define LOAD_ERROR(MSG) \
76 printk ("addrspace_load: %s: ", filename); \
83 install_page (struct addrspace *as, void *upage, void *kpage)
85 /* Verify that there's not already a page at that virtual
86 address, then map our page there. */
87 if (pagedir_get_page (as->pagedir, upage) == NULL
88 && pagedir_set_page (as->pagedir, upage, kpage, true))
98 load_segment (struct addrspace *as, struct file *file,
99 const struct Elf32_Phdr *phdr)
106 ASSERT (file != NULL);
107 ASSERT (phdr != NULL);
108 ASSERT (phdr->p_type == PT_LOAD);
110 /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
111 if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE)
113 printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
114 phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
118 /* p_memsz must be at least as big as p_filesz. */
119 if (phdr->p_memsz < phdr->p_filesz)
121 printk ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
122 phdr->p_memsz, phdr->p_filesz);
126 /* Validate virtual memory region to be mapped. */
127 start = pg_round_down ((void *) phdr->p_vaddr);
128 end = pg_round_up ((void *) (phdr->p_vaddr + phdr->p_memsz));
129 if (start >= PHYS_BASE || end >= PHYS_BASE || end < start)
131 printk ("bad virtual region %08lx...%08lx\n",
132 (unsigned long) start, (unsigned long) end);
136 filesz_left = phdr->p_filesz + (phdr->p_vaddr & PGMASK);
137 file_seek (file, ROUND_DOWN (phdr->p_offset, PGSIZE));
138 for (upage = start; upage < (uint8_t *) end; upage += PGSIZE)
140 size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
141 size_t zero_bytes = PGSIZE - read_bytes;
142 uint8_t *kpage = palloc_get (0);
146 if (file_read (file, kpage, read_bytes) != (int) read_bytes)
148 memset (kpage + read_bytes, 0, zero_bytes);
149 filesz_left -= read_bytes;
151 if (!install_page (as, upage, kpage))
159 setup_stack (struct addrspace *as)
161 uint8_t *kpage = palloc_get (PAL_ZERO);
164 printk ("failed to allocate process stack\n");
168 return install_page (as, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
172 addrspace_load (struct addrspace *as, const char *filename,
173 void (**start) (void))
175 struct Elf32_Ehdr ehdr;
176 struct file *file = NULL;
178 bool success = false;
181 as->pagedir = pagedir_create ();
183 file = filesys_open (filename);
185 LOAD_ERROR (("open failed"));
187 /* Read and verify executable header. */
188 if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr)
189 LOAD_ERROR (("error reading executable header"));
190 if (memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) != 0)
191 LOAD_ERROR (("file is not ELF"));
192 if (ehdr.e_type != 2)
193 LOAD_ERROR (("ELF file is not an executable"));
194 if (ehdr.e_machine != 3)
195 LOAD_ERROR (("ELF executable is not x86"));
196 if (ehdr.e_version != 1)
197 LOAD_ERROR (("ELF executable hasunknown version %d",
198 (int) ehdr.e_version));
199 if (ehdr.e_phentsize != sizeof (struct Elf32_Phdr))
200 LOAD_ERROR (("bad ELF program header size"));
201 if (ehdr.e_phnum > 1024)
202 LOAD_ERROR (("too many ELF program headers"));
204 /* Read program headers. */
205 file_ofs = ehdr.e_phoff;
206 for (i = 0; i < ehdr.e_phnum; i++)
208 struct Elf32_Phdr phdr;
210 file_seek (file, file_ofs);
211 if (file_read (file, &phdr, sizeof phdr) != sizeof phdr)
212 LOAD_ERROR (("error reading program header"));
213 file_ofs += sizeof phdr;
220 /* Ignore this segment. */
225 /* Reject the executable. */
226 LOAD_ERROR (("unsupported ELF segment type %d\n", phdr.p_type));
229 printk ("unknown ELF segment type %08x\n", phdr.p_type);
232 if (!load_segment (as, file, &phdr))
239 if (!setup_stack (as))
243 *start = (void (*) (void)) ehdr.e_entry;
251 addrspace_destroy (as);
256 addrspace_destroy (struct addrspace *as)
258 if (as != NULL && as->pagedir != NULL)
259 pagedir_destroy (as->pagedir);
263 addrspace_activate (struct addrspace *as)
267 if (as->pagedir != NULL)
268 pagedir_activate (as->pagedir);
269 tss->esp0 = (uint32_t) pg_round_down (as) + PGSIZE;