Add comments.
[pintos-anon] / src / userprog / addrspace.c
1 #include "addrspace.h"
2 #include <inttypes.h>
3 #include "debug.h"
4 #include "file.h"
5 #include "filesys.h"
6 #include "init.h"
7 #include "lib.h"
8 #include "mmu.h"
9 #include "paging.h"
10 #include "palloc.h"
11 #include "thread.h"
12 #include "tss.h"
13
14 /* We load ELF binaries.  The following definitions are taken
15    from the ELF specification, [ELF], more-or-less verbatim.  */
16
17 /* ELF types. */
18 typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
19 typedef uint16_t Elf32_Half;
20
21 #define PE32Wx PRIx32
22 #define PE32Ax PRIx32
23 #define PE32Ox PRIx32
24 #define PE32Hx PRIx16
25
26 /* Executable header.
27    This appears at the very beginning of an ELF binary. */
28 struct Elf32_Ehdr
29   {
30     unsigned char e_ident[16];
31     Elf32_Half    e_type;
32     Elf32_Half    e_machine;
33     Elf32_Word    e_version;
34     Elf32_Addr    e_entry;
35     Elf32_Off     e_phoff;
36     Elf32_Off     e_shoff;
37     Elf32_Word    e_flags;
38     Elf32_Half    e_ehsize;
39     Elf32_Half    e_phentsize;
40     Elf32_Half    e_phnum;
41     Elf32_Half    e_shentsize;
42     Elf32_Half    e_shnum;
43     Elf32_Half    e_shstrndx;
44   };
45
46 /* Program header.
47    There are e_phnum of these, starting at file offset e_phoff. */
48 struct Elf32_Phdr
49   {
50     Elf32_Word p_type;
51     Elf32_Off  p_offset;
52     Elf32_Addr p_vaddr;
53     Elf32_Addr p_paddr;
54     Elf32_Word p_filesz;
55     Elf32_Word p_memsz;
56     Elf32_Word p_flags;
57     Elf32_Word p_align;
58   };
59
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. */
69
70 /* Flags for p_flags. */
71 #define PF_X 1          /* Executable. */
72 #define PF_W 2          /* Writable. */
73 #define PF_R 4          /* Readable. */
74
75 static bool install_page (struct thread *, void *upage, void *kpage);
76 static bool load_segment (struct thread *, struct file *,
77                           const struct Elf32_Phdr *);
78 static bool setup_stack (struct thread *);
79
80 /* Aborts loading an executable, with an error message. */
81 #define LOAD_ERROR(MSG)                                         \
82         do {                                                    \
83                 printk ("addrspace_load: %s: ", filename);      \
84                 printk MSG;                                     \
85                 printk ("\n");                                  \
86                 goto error;                                     \
87         } while (0)
88
89 bool
90 addrspace_load (struct thread *t, const char *filename,
91                 void (**start) (void)) 
92 {
93   struct Elf32_Ehdr ehdr;
94   struct file file;
95   bool file_open = false;
96   off_t file_ofs;
97   bool success = false;
98   int i;
99
100   /* Allocate page directory. */
101   t->pagedir = pagedir_create ();
102   if (t->pagedir == NULL)
103     LOAD_ERROR (("page directory allocation failed"));
104
105   /* Open executable file. */
106   file_open = filesys_open (filename, &file);
107   if (!file_open)
108     LOAD_ERROR (("open failed"));
109
110   /* Read and verify executable header. */
111   if (file_read (&file, &ehdr, sizeof ehdr) != sizeof ehdr) 
112     LOAD_ERROR (("error reading executable header"));
113   if (memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) != 0)
114     LOAD_ERROR (("file is not ELF"));
115   if (ehdr.e_type != 2)
116     LOAD_ERROR (("ELF file is not an executable"));
117   if (ehdr.e_machine != 3)
118     LOAD_ERROR (("ELF executable is not x86"));
119   if (ehdr.e_version != 1)
120     LOAD_ERROR (("ELF executable hasunknown version %d",
121                  (int) ehdr.e_version));
122   if (ehdr.e_phentsize != sizeof (struct Elf32_Phdr))
123     LOAD_ERROR (("bad ELF program header size"));
124   if (ehdr.e_phnum > 1024)
125     LOAD_ERROR (("too many ELF program headers"));
126
127   /* Read program headers. */
128   file_ofs = ehdr.e_phoff;
129   for (i = 0; i < ehdr.e_phnum; i++) 
130     {
131       struct Elf32_Phdr phdr;
132
133       file_seek (&file, file_ofs);
134       if (file_read (&file, &phdr, sizeof phdr) != sizeof phdr)
135         LOAD_ERROR (("error reading program header"));
136       file_ofs += sizeof phdr;
137       switch (phdr.p_type) 
138         {
139         case PT_NULL:
140         case PT_NOTE:
141         case PT_PHDR:
142         case PT_STACK:
143           /* Ignore this segment. */
144           break;
145         case PT_DYNAMIC:
146         case PT_INTERP:
147         case PT_SHLIB:
148           /* Reject the executable. */
149           LOAD_ERROR (("unsupported ELF segment type %d\n", phdr.p_type));
150           break;
151         default:
152           printk ("unknown ELF segment type %08x\n", phdr.p_type);
153           break;
154         case PT_LOAD:
155           if (!load_segment (t, &file, &phdr))
156             goto error;
157           break;
158         }
159     }
160
161   /* Set up stack. */
162   if (!setup_stack (t))
163     goto error;
164
165   /* Start address. */
166   *start = (void (*) (void)) ehdr.e_entry;
167
168   success = true;
169
170  error:
171   if (file_open)
172     file_close (&file);
173   if (!success) 
174     addrspace_destroy (t);
175   return success;
176 }
177
178 void
179 addrspace_destroy (struct thread *t)
180 {
181   if (t->pagedir != NULL) 
182     {
183       pagedir_destroy (t->pagedir);
184       t->pagedir = NULL; 
185     }
186 }
187
188 void
189 addrspace_activate (struct thread *t)
190 {
191   ASSERT (t != NULL);
192   
193   pagedir_activate (t->pagedir);
194   tss_set_esp0 ((uint8_t *) t + PGSIZE);
195 }
196 \f
197 /* addrspace_load() helpers. */
198
199 static bool
200 install_page (struct thread *t, void *upage, void *kpage)
201 {
202   /* Verify that there's not already a page at that virtual
203      address, then map our page there. */
204   if (pagedir_get_page (t->pagedir, upage) == NULL
205       && pagedir_set_page (t->pagedir, upage, kpage, true))
206     return true;
207   else
208     {
209       palloc_free (kpage);
210       return false;
211     }
212 }
213
214 static bool
215 load_segment (struct thread *t, struct file *file,
216               const struct Elf32_Phdr *phdr) 
217 {
218   void *start, *end;
219   uint8_t *upage;
220   off_t filesz_left;
221
222   ASSERT (t != NULL);
223   ASSERT (file != NULL);
224   ASSERT (phdr != NULL);
225   ASSERT (phdr->p_type == PT_LOAD);
226
227   /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
228   if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE) 
229     {
230       printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
231               phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
232       return false; 
233     }
234
235   /* p_memsz must be at least as big as p_filesz. */
236   if (phdr->p_memsz < phdr->p_filesz) 
237     {
238       printk ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
239               phdr->p_memsz, phdr->p_filesz);
240       return false; 
241     }
242
243   /* Validate virtual memory region to be mapped. */
244   start = pg_round_down ((void *) phdr->p_vaddr);
245   end = pg_round_up ((void *) (phdr->p_vaddr + phdr->p_memsz));
246   if (start >= PHYS_BASE || end >= PHYS_BASE || end < start) 
247     {
248       printk ("bad virtual region %08lx...%08lx\n",
249               (unsigned long) start, (unsigned long) end);
250       return false; 
251     }
252
253   filesz_left = phdr->p_filesz + (phdr->p_vaddr & PGMASK);
254   file_seek (file, ROUND_DOWN (phdr->p_offset, PGSIZE));
255   for (upage = start; upage < (uint8_t *) end; upage += PGSIZE) 
256     {
257       size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
258       size_t zero_bytes = PGSIZE - read_bytes;
259       uint8_t *kpage = palloc_get (0);
260       if (kpage == NULL)
261         return false;
262
263       if (file_read (file, kpage, read_bytes) != (int) read_bytes)
264         return false;
265       memset (kpage + read_bytes, 0, zero_bytes);
266       filesz_left -= read_bytes;
267
268       if (!install_page (t, upage, kpage))
269         return false;
270     }
271
272   return true;
273 }
274
275 static bool
276 setup_stack (struct thread *t) 
277 {
278   uint8_t *kpage = palloc_get (PAL_ZERO);
279   if (kpage == NULL)
280     {
281       printk ("failed to allocate process stack\n");
282       return false;
283     }
284
285   return install_page (t, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
286 }
287