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