Break GDT, TSS out of init.c, mmu.h.
[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 "gdt.h"
7 #include "init.h"
8 #include "lib.h"
9 #include "mmu.h"
10 #include "paging.h"
11 #include "palloc.h"
12 #include "thread.h"
13
14 /* We load ELF binaries.  The following definitions are taken
15    from the ELF specification 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 #define LOAD_ERROR(MSG)                                         \
76         do {                                                    \
77                 printk ("addrspace_load: %s: ", filename);      \
78                 printk MSG;                                     \
79                 printk ("\n");                                  \
80                 goto error;                                     \
81         } while (0)
82
83 static bool
84 install_page (struct thread *t, void *upage, void *kpage)
85 {
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))
90     return true;
91   else
92     {
93       palloc_free (kpage);
94       return false;
95     }
96 }
97
98 static bool
99 load_segment (struct thread *t, struct file *file,
100               const struct Elf32_Phdr *phdr) 
101 {
102   void *start, *end;
103   uint8_t *upage;
104   off_t filesz_left;
105
106   ASSERT (t != NULL);
107   ASSERT (file != NULL);
108   ASSERT (phdr != NULL);
109   ASSERT (phdr->p_type == PT_LOAD);
110
111   /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
112   if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE) 
113     {
114       printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
115               phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
116       return false; 
117     }
118
119   /* p_memsz must be at least as big as p_filesz. */
120   if (phdr->p_memsz < phdr->p_filesz) 
121     {
122       printk ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
123               phdr->p_memsz, phdr->p_filesz);
124       return false; 
125     }
126
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) 
131     {
132       printk ("bad virtual region %08lx...%08lx\n",
133               (unsigned long) start, (unsigned long) end);
134       return false; 
135     }
136
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) 
140     {
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);
144       if (kpage == NULL)
145         return false;
146
147       if (file_read (file, kpage, read_bytes) != (int) read_bytes)
148         return false;
149       memset (kpage + read_bytes, 0, zero_bytes);
150       filesz_left -= read_bytes;
151
152       if (!install_page (t, upage, kpage))
153         return false;
154     }
155
156   return true;
157 }
158
159 static bool
160 setup_stack (struct thread *t) 
161 {
162   uint8_t *kpage = palloc_get (PAL_ZERO);
163   if (kpage == NULL)
164     {
165       printk ("failed to allocate process stack\n");
166       return false;
167     }
168
169   return install_page (t, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
170 }
171
172 bool
173 addrspace_load (struct thread *t, const char *filename,
174                 void (**start) (void)) 
175 {
176   struct Elf32_Ehdr ehdr;
177   struct file file;
178   bool file_open = false;
179   off_t file_ofs;
180   bool success = false;
181   int i;
182
183   /* Allocate page directory. */
184   t->pagedir = pagedir_create ();
185   if (t->pagedir == NULL)
186     LOAD_ERROR (("page directory allocation failed"));
187
188   /* Open executable file. */
189   file_open = filesys_open (filename, &file);
190   if (!file_open)
191     LOAD_ERROR (("open failed"));
192
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"));
209
210   /* Read program headers. */
211   file_ofs = ehdr.e_phoff;
212   for (i = 0; i < ehdr.e_phnum; i++) 
213     {
214       struct Elf32_Phdr phdr;
215
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;
220       switch (phdr.p_type) 
221         {
222         case PT_NULL:
223         case PT_NOTE:
224         case PT_PHDR:
225         case PT_STACK:
226           /* Ignore this segment. */
227           break;
228         case PT_DYNAMIC:
229         case PT_INTERP:
230         case PT_SHLIB:
231           /* Reject the executable. */
232           LOAD_ERROR (("unsupported ELF segment type %d\n", phdr.p_type));
233           break;
234         default:
235           printk ("unknown ELF segment type %08x\n", phdr.p_type);
236           break;
237         case PT_LOAD:
238           if (!load_segment (t, &file, &phdr))
239             goto error;
240           break;
241         }
242     }
243
244   /* Set up stack. */
245   if (!setup_stack (t))
246     goto error;
247
248   /* Start address. */
249   *start = (void (*) (void)) ehdr.e_entry;
250
251   success = true;
252
253  error:
254   if (file_open)
255     file_close (&file);
256   if (!success) 
257     addrspace_destroy (t);
258   return success;
259 }
260
261 void
262 addrspace_destroy (struct thread *t)
263 {
264   if (t->pagedir != NULL) 
265     {
266       pagedir_destroy (t->pagedir);
267       t->pagedir = NULL; 
268     }
269 }
270
271 void
272 addrspace_activate (struct thread *t)
273 {
274   ASSERT (t != NULL);
275   
276   if (t->pagedir != NULL)
277     pagedir_activate (t->pagedir);
278   tss->esp0 = (uint32_t) t + PGSIZE;
279 }