Improve 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 "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 load_segment (struct addrspace *as, struct file *file,
83               const struct Elf32_Phdr *phdr) 
84 {
85   void *start, *end;
86   uint8_t *upage;
87   off_t filesz_left;
88
89   ASSERT (as != NULL);
90   ASSERT (file != NULL);
91   ASSERT (phdr != NULL);
92   ASSERT (phdr->p_type == PT_LOAD);
93
94   /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
95   if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE) 
96     {
97       printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
98               phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
99       return false; 
100     }
101
102   /* p_memsz must be at least as big as p_filesz. */
103   if (phdr->p_memsz < phdr->p_filesz) 
104     {
105       printk ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
106               phdr->p_memsz, phdr->p_filesz);
107       return false; 
108     }
109
110   /* Validate virtual memory region to be mapped. */
111   start = pg_round_down ((void *) phdr->p_vaddr);
112   end = pg_round_up ((void *) (phdr->p_vaddr + phdr->p_memsz));
113   if (start >= PHYS_BASE || end >= PHYS_BASE || end < start) 
114     {
115       printk ("bad virtual region %08lx...%08lx\n",
116               (unsigned long) start, (unsigned long) end);
117       return false; 
118     }
119
120   filesz_left = phdr->p_filesz + (phdr->p_vaddr & PGMASK);
121   file_seek (file, ROUND_DOWN (phdr->p_offset, PGSIZE));
122   for (upage = start; upage < (uint8_t *) end; upage += PGSIZE) 
123     {
124       size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
125       size_t zero_bytes = PGSIZE - read_bytes;
126       uint8_t *kpage = palloc_get (0);
127       if (kpage == NULL)
128         return false;
129
130       if (file_read (file, kpage, read_bytes) != (int) read_bytes)
131         return false;
132       memset (kpage + read_bytes, 0, zero_bytes);
133       filesz_left -= read_bytes;
134
135       if (pagedir_get_page (as->pagedir, upage))
136         {
137           palloc_free (kpage);
138           return false;
139         }
140       pagedir_set_page (as->pagedir, upage, kpage, true);
141     }
142
143   return true;
144 }
145
146 bool
147 addrspace_load (struct addrspace *as, const char *filename) 
148 {
149   struct Elf32_Ehdr ehdr;
150   struct file *file = NULL;
151   off_t file_ofs;
152   bool success = false;
153   int i;
154
155   as->pagedir = pagedir_create ();
156
157   file = filesys_open (filename);
158   if (file == NULL)
159     LOAD_ERROR (("open failed"));
160
161   /* Read and verify executable header. */
162   if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr) 
163     LOAD_ERROR (("error reading executable header"));
164   if (memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) != 0)
165     LOAD_ERROR (("file is not ELF"));
166   if (ehdr.e_type != 2)
167     LOAD_ERROR (("ELF file is not an executable"));
168   if (ehdr.e_machine != 3)
169     LOAD_ERROR (("ELF executable is not x86"));
170   if (ehdr.e_version != 1)
171     LOAD_ERROR (("ELF executable hasunknown version %d",
172                  (int) ehdr.e_version));
173   if (ehdr.e_phentsize != sizeof (struct Elf32_Phdr))
174     LOAD_ERROR (("bad ELF program header size"));
175   if (ehdr.e_phnum > 1024)
176     LOAD_ERROR (("too many ELF program headers"));
177
178   /* Read program headers. */
179   file_ofs = ehdr.e_phoff;
180   printk ("e_phnum=%d\n", ehdr.e_phnum);
181   for (i = 0; i < ehdr.e_phnum; i++) 
182     {
183       struct Elf32_Phdr phdr;
184
185       file_seek (file, file_ofs);
186       if (file_read (file, &phdr, sizeof phdr) != sizeof phdr)
187         LOAD_ERROR (("error reading program header"));
188       printk ("%x: %08x, %08x %08x %08x %05x %05x\n",
189               file_tell (file),
190               phdr.p_type,
191               phdr.p_offset, phdr.p_vaddr, phdr.p_paddr,
192               phdr.p_filesz, phdr.p_memsz);
193       file_ofs += sizeof phdr;
194       switch (phdr.p_type) 
195         {
196         case PT_NULL:
197         case PT_NOTE:
198         case PT_PHDR:
199         case PT_STACK: /* Stack segment. */
200           /* Ignore this segment. */
201           break;
202         case PT_DYNAMIC:
203         case PT_INTERP:
204         case PT_SHLIB:
205           /* Reject the executable. */
206           LOAD_ERROR (("unsupported ELF segment type %d\n", phdr.p_type));
207           break;
208         default:
209           printk ("unknown ELF segment type %08x\n", phdr.p_type);
210           break;
211         case PT_LOAD:
212           if (!load_segment (as, file, &phdr))
213             goto error;
214           break;
215         }
216     }
217   success = true;
218
219  error:
220   if (file != NULL)
221     file_close (file);
222   if (!success) 
223     addrspace_destroy (as);
224   return success;
225 }
226
227 void
228 addrspace_destroy (struct addrspace *as)
229 {
230   if (as != NULL && as->pagedir != NULL) 
231     pagedir_destroy (as->pagedir); 
232 }