12 /* We load ELF binaries. The following definitions are taken
13 from the ELF specification more-or-less verbatim. */
16 typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
17 typedef uint16_t Elf32_Half;
25 This appears at the very beginning of an ELF binary. */
28 unsigned char e_ident[16];
37 Elf32_Half e_phentsize;
39 Elf32_Half e_shentsize;
41 Elf32_Half e_shstrndx;
45 There are e_phnum of these, starting at file offset e_phoff. */
58 /* Values for p_type. */
59 #define PT_NULL 0 /* Ignore. */
60 #define PT_LOAD 1 /* Loadable segment. */
61 #define PT_DYNAMIC 2 /* Dynamic linking info. */
62 #define PT_INTERP 3 /* Name of dynamic loader. */
63 #define PT_NOTE 4 /* Auxiliary info. */
64 #define PT_SHLIB 5 /* Reserved. */
65 #define PT_PHDR 6 /* Program header table. */
66 #define PT_STACK 0x6474e551 /* Stack segment. */
68 /* Flags for p_flags. */
69 #define PF_X 1 /* Executable. */
70 #define PF_W 2 /* Writable. */
71 #define PF_R 4 /* Readable. */
73 #define LOAD_ERROR(MSG) \
75 printk ("addrspace_load: %s: ", filename); \
82 install_page (struct addrspace *as, void *upage, void *kpage)
84 /* Verify that there's not already a page at that virtual
85 address, then map our page there. */
86 if (pagedir_get_page (as->pagedir, upage) == NULL
87 && pagedir_set_page (as->pagedir, upage, kpage, true))
97 load_segment (struct addrspace *as, struct file *file,
98 const struct Elf32_Phdr *phdr)
105 ASSERT (file != NULL);
106 ASSERT (phdr != NULL);
107 ASSERT (phdr->p_type == PT_LOAD);
109 /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
110 if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE)
112 printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
113 phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
117 /* p_memsz must be at least as big as p_filesz. */
118 if (phdr->p_memsz < phdr->p_filesz)
120 printk ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
121 phdr->p_memsz, phdr->p_filesz);
125 /* Validate virtual memory region to be mapped. */
126 start = pg_round_down ((void *) phdr->p_vaddr);
127 end = pg_round_up ((void *) (phdr->p_vaddr + phdr->p_memsz));
128 if (start >= PHYS_BASE || end >= PHYS_BASE || end < start)
130 printk ("bad virtual region %08lx...%08lx\n",
131 (unsigned long) start, (unsigned long) end);
135 filesz_left = phdr->p_filesz + (phdr->p_vaddr & PGMASK);
136 file_seek (file, ROUND_DOWN (phdr->p_offset, PGSIZE));
137 for (upage = start; upage < (uint8_t *) end; upage += PGSIZE)
139 size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
140 size_t zero_bytes = PGSIZE - read_bytes;
141 uint8_t *kpage = palloc_get (0);
145 if (file_read (file, kpage, read_bytes) != (int) read_bytes)
147 memset (kpage + read_bytes, 0, zero_bytes);
148 filesz_left -= read_bytes;
150 if (!install_page (as, upage, kpage))
158 setup_stack (struct addrspace *as)
160 uint8_t *kpage = palloc_get (PAL_ZERO);
163 printk ("failed to allocate process stack\n");
167 return install_page (as, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
171 addrspace_load (struct addrspace *as, const char *filename,
172 void (**start) (void))
174 struct Elf32_Ehdr ehdr;
176 bool file_open = false;
178 bool success = false;
181 as->pagedir = pagedir_create ();
183 file_open = filesys_open (filename, &file);
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;