Make userspace actually work.
[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 "malloc.h"
10 #include "paging.h"
11 #include "palloc.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 addrspace *as, 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 (as->pagedir, upage) == NULL
88       && pagedir_set_page (as->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 addrspace *as, 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 (as != 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 (as, upage, kpage))
152         return false;
153     }
154
155   return true;
156 }
157
158 static bool
159 setup_stack (struct addrspace *as) 
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 (as, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
169 }
170
171 bool
172 addrspace_load (struct addrspace *as, const char *filename,
173                 void (**start) (void)) 
174 {
175   struct Elf32_Ehdr ehdr;
176   struct file *file = NULL;
177   off_t file_ofs;
178   bool success = false;
179   int i;
180
181   as->pagedir = pagedir_create ();
182
183   file = filesys_open (filename);
184   if (file == NULL)
185     LOAD_ERROR (("open failed"));
186
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"));
203
204   /* Read program headers. */
205   file_ofs = ehdr.e_phoff;
206   for (i = 0; i < ehdr.e_phnum; i++) 
207     {
208       struct Elf32_Phdr phdr;
209
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;
214       switch (phdr.p_type) 
215         {
216         case PT_NULL:
217         case PT_NOTE:
218         case PT_PHDR:
219         case PT_STACK:
220           /* Ignore this segment. */
221           break;
222         case PT_DYNAMIC:
223         case PT_INTERP:
224         case PT_SHLIB:
225           /* Reject the executable. */
226           LOAD_ERROR (("unsupported ELF segment type %d\n", phdr.p_type));
227           break;
228         default:
229           printk ("unknown ELF segment type %08x\n", phdr.p_type);
230           break;
231         case PT_LOAD:
232           if (!load_segment (as, file, &phdr))
233             goto error;
234           break;
235         }
236     }
237
238   /* Set up stack. */
239   if (!setup_stack (as))
240     goto error;
241
242   /* Start address. */
243   *start = (void (*) (void)) ehdr.e_entry;
244
245   success = true;
246
247  error:
248   if (file != NULL)
249     file_close (file);
250   if (!success) 
251     addrspace_destroy (as);
252   return success;
253 }
254
255 void
256 addrspace_destroy (struct addrspace *as)
257 {
258   if (as != NULL && as->pagedir != NULL) 
259     pagedir_destroy (as->pagedir); 
260 }
261
262 void
263 addrspace_activate (struct addrspace *as) 
264 {
265   ASSERT (as != NULL);
266   
267   if (as->pagedir != NULL)
268     pagedir_activate (as->pagedir);
269   tss->esp0 = (uint32_t) pg_round_down (as) + PGSIZE;
270 }