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