14 /* We load ELF binaries. The following definitions are taken
15 from the ELF specification more-or-less verbatim. */
18 typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
19 typedef uint16_t Elf32_Half;
27 This appears at the very beginning of an ELF binary. */
30 unsigned char e_ident[16];
39 Elf32_Half e_phentsize;
41 Elf32_Half e_shentsize;
43 Elf32_Half e_shstrndx;
47 There are e_phnum of these, starting at file offset e_phoff. */
60 /* Values for p_type. */
61 #define PT_NULL 0 /* Ignore. */
62 #define PT_LOAD 1 /* Loadable segment. */
63 #define PT_DYNAMIC 2 /* Dynamic linking info. */
64 #define PT_INTERP 3 /* Name of dynamic loader. */
65 #define PT_NOTE 4 /* Auxiliary info. */
66 #define PT_SHLIB 5 /* Reserved. */
67 #define PT_PHDR 6 /* Program header table. */
68 #define PT_STACK 0x6474e551 /* Stack segment. */
70 /* Flags for p_flags. */
71 #define PF_X 1 /* Executable. */
72 #define PF_W 2 /* Writable. */
73 #define PF_R 4 /* Readable. */
75 #define LOAD_ERROR(MSG) \
77 printk ("addrspace_load: %s: ", filename); \
84 install_page (struct thread *t, void *upage, void *kpage)
86 /* Verify that there's not already a page at that virtual
87 address, then map our page there. */
88 if (pagedir_get_page (t->pagedir, upage) == NULL
89 && pagedir_set_page (t->pagedir, upage, kpage, true))
99 load_segment (struct thread *t, struct file *file,
100 const struct Elf32_Phdr *phdr)
107 ASSERT (file != NULL);
108 ASSERT (phdr != NULL);
109 ASSERT (phdr->p_type == PT_LOAD);
111 /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
112 if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE)
114 printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
115 phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
119 /* p_memsz must be at least as big as p_filesz. */
120 if (phdr->p_memsz < phdr->p_filesz)
122 printk ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
123 phdr->p_memsz, phdr->p_filesz);
127 /* Validate virtual memory region to be mapped. */
128 start = pg_round_down ((void *) phdr->p_vaddr);
129 end = pg_round_up ((void *) (phdr->p_vaddr + phdr->p_memsz));
130 if (start >= PHYS_BASE || end >= PHYS_BASE || end < start)
132 printk ("bad virtual region %08lx...%08lx\n",
133 (unsigned long) start, (unsigned long) end);
137 filesz_left = phdr->p_filesz + (phdr->p_vaddr & PGMASK);
138 file_seek (file, ROUND_DOWN (phdr->p_offset, PGSIZE));
139 for (upage = start; upage < (uint8_t *) end; upage += PGSIZE)
141 size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
142 size_t zero_bytes = PGSIZE - read_bytes;
143 uint8_t *kpage = palloc_get (0);
147 if (file_read (file, kpage, read_bytes) != (int) read_bytes)
149 memset (kpage + read_bytes, 0, zero_bytes);
150 filesz_left -= read_bytes;
152 if (!install_page (t, upage, kpage))
160 setup_stack (struct thread *t)
162 uint8_t *kpage = palloc_get (PAL_ZERO);
165 printk ("failed to allocate process stack\n");
169 return install_page (t, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
173 addrspace_load (struct thread *t, const char *filename,
174 void (**start) (void))
176 struct Elf32_Ehdr ehdr;
178 bool file_open = false;
180 bool success = false;
183 /* Allocate page directory. */
184 t->pagedir = pagedir_create ();
185 if (t->pagedir == NULL)
186 LOAD_ERROR (("page directory allocation failed"));
188 /* Open executable file. */
189 file_open = filesys_open (filename, &file);
191 LOAD_ERROR (("open failed"));
193 /* Read and verify executable header. */
194 if (file_read (&file, &ehdr, sizeof ehdr) != sizeof ehdr)
195 LOAD_ERROR (("error reading executable header"));
196 if (memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) != 0)
197 LOAD_ERROR (("file is not ELF"));
198 if (ehdr.e_type != 2)
199 LOAD_ERROR (("ELF file is not an executable"));
200 if (ehdr.e_machine != 3)
201 LOAD_ERROR (("ELF executable is not x86"));
202 if (ehdr.e_version != 1)
203 LOAD_ERROR (("ELF executable hasunknown version %d",
204 (int) ehdr.e_version));
205 if (ehdr.e_phentsize != sizeof (struct Elf32_Phdr))
206 LOAD_ERROR (("bad ELF program header size"));
207 if (ehdr.e_phnum > 1024)
208 LOAD_ERROR (("too many ELF program headers"));
210 /* Read program headers. */
211 file_ofs = ehdr.e_phoff;
212 for (i = 0; i < ehdr.e_phnum; i++)
214 struct Elf32_Phdr phdr;
216 file_seek (&file, file_ofs);
217 if (file_read (&file, &phdr, sizeof phdr) != sizeof phdr)
218 LOAD_ERROR (("error reading program header"));
219 file_ofs += sizeof phdr;
226 /* Ignore this segment. */
231 /* Reject the executable. */
232 LOAD_ERROR (("unsupported ELF segment type %d\n", phdr.p_type));
235 printk ("unknown ELF segment type %08x\n", phdr.p_type);
238 if (!load_segment (t, &file, &phdr))
245 if (!setup_stack (t))
249 *start = (void (*) (void)) ehdr.e_entry;
257 addrspace_destroy (t);
262 addrspace_destroy (struct thread *t)
264 if (t->pagedir != NULL)
266 pagedir_destroy (t->pagedir);
272 addrspace_activate (struct thread *t)
276 if (t->pagedir != NULL)
277 pagedir_activate (t->pagedir);
278 tss_set_esp0 ((uint8_t *) t + PGSIZE);