mm.h directly includes ros/mman.h
[akaros.git] / kern / src / elf.c
1 #include <mm.h>
2 #include <frontend.h>
3 #include <string.h>
4 #include <kmalloc.h>
5 #include <syscall.h>
6 #include <elf.h>
7 #include <pmap.h>
8 #include <smp.h>
9 #include <arch/arch.h>
10
11 #ifdef KERN64
12 # define elf_field(obj, field) (elf64 ? (obj##64)->field : (obj##32)->field)
13 #else
14 # define elf_field(obj, field) ((obj##32)->field)
15 #endif
16
17 static int load_one_elf(struct proc *p, struct file *f, int pgoffset,
18                         elf_info_t *ei)
19 {
20         int ret = -1;
21         ei->phdr = -1;
22         ei->dynamic = 0;
23         ei->highest_addr = 0;
24         off_t f_off = 0;
25
26         /* assume program headers fit in a page.  if this isn't true, change the
27          * code below that maps in program headers */
28         char* elf = (char*)kmalloc(PGSIZE, 0);
29         
30         /* When reading on behalf of the kernel, we need to make sure no proc is
31          * "current".  This is a bit ghetto (TODO: KFOP) */
32         struct proc *cur_proc = current;
33         current = 0;
34         if (!elf || f->f_op->read(f, elf, PGSIZE, &f_off) == -1)
35                 goto fail;
36         current = cur_proc;
37
38         elf32_t* elfhdr32 = (elf32_t*)elf;
39         elf64_t* elfhdr64 = (elf64_t*)elf;
40         bool elf32 = elfhdr32->e_ident[ELF_IDENT_CLASS] == ELFCLASS32;
41         bool elf64 = elfhdr32->e_ident[ELF_IDENT_CLASS] == ELFCLASS64;
42         if (!elf64 && !elf32)
43                 goto fail;
44         #ifndef KERN64
45         if(elf64)
46                 goto fail;
47         #endif
48         
49         proghdr32_t* proghdrs32 = (proghdr32_t*)(elf + elfhdr32->e_phoff);
50         proghdr64_t* proghdrs64 = (proghdr64_t*)(elf + elfhdr64->e_phoff);
51         uintptr_t e_phoff = elf_field(elfhdr, e_phoff);
52         size_t phsz = elf64 ? sizeof(proghdr64_t) : sizeof(proghdr32_t);
53         uint16_t e_phnum = elf_field(elfhdr, e_phnum);
54         // we don't support prog hdrs extending past the first elf page
55         if (e_phoff + e_phnum * phsz > PGSIZE)
56                 goto fail;
57
58         for (int i = 0; i < e_phnum; i++) {
59                 proghdr32_t* ph32 = proghdrs32+i;
60                 proghdr64_t* ph64 = proghdrs64+i;
61                 uint16_t p_type = elf_field(ph, p_type);
62                 uintptr_t p_va = elf_field(ph, p_va);
63                 uintptr_t p_offset = elf_field(ph, p_offset);
64                 uintptr_t p_align = elf_field(ph, p_align);
65                 uintptr_t p_memsz = elf_field(ph, p_memsz);
66                 uintptr_t p_filesz = elf_field(ph, p_filesz);
67
68                 if (p_type == ELF_PROG_PHDR)
69                         ei->phdr = elf_field(ph, p_va);
70                 if (p_type == ELF_PROG_INTERP) {
71                         int maxlen = MIN(PGSIZE - p_offset, sizeof(ei->interp));
72                         int len = strnlen(elf + p_offset, maxlen);
73                         if (len < maxlen) {
74                                 memcpy(ei->interp, elf + p_offset, maxlen + 1);
75                                 ei->dynamic = 1;
76                         }
77                         else
78                                 goto fail;
79                 }
80
81                 if (p_type == ELF_PROG_LOAD && p_memsz) {
82                         if (p_align % PGSIZE)
83                                 goto fail;
84                         if (p_offset % PGSIZE != p_va % PGSIZE)
85                                 goto fail;
86
87                         uintptr_t filestart = ROUNDDOWN(p_offset, PGSIZE);
88                         uintptr_t fileend = p_offset + p_filesz;
89                         uintptr_t filesz = fileend - filestart;
90
91                         uintptr_t memstart = ROUNDDOWN(p_va, PGSIZE);
92                         uintptr_t memend = ROUNDUP(p_va + p_memsz, PGSIZE);
93                         uintptr_t memsz = memend - memstart;
94                         if (memend > ei->highest_addr)
95                                 ei->highest_addr = memend;
96                         /* This needs to be a PRIVATE mapping, and the stuff after the file
97                          * needs to be zeroed. */
98                         if (filesz) {
99                                 /* TODO: figure out proper permissions from the elf */
100                                 if (do_mmap(p, memstart + pgoffset * PGSIZE, filesz,
101                                            PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_PRIVATE,
102                                            f, filestart) == MAP_FAILED)
103                                         goto fail;
104                                 /* Due to elf-ghetto-ness, we need to zero the first part of the
105                                  * BSS from the last page of the data segment.  We translate to
106                                  * the KVA so we don't need to worry about using the proc's
107                                  * mapping */
108                                 uintptr_t z_s = memstart + pgoffset * PGSIZE + filesz;
109                                 pte_t *pte = pgdir_walk(p->env_pgdir, (void*)z_s, 0);
110                                 assert(pte);
111                                 uintptr_t kva_z_s = (uintptr_t)ppn2kva(PTE2PPN(*pte)) + PGOFF(z_s);
112                                 uintptr_t kva_z_e = ROUNDUP(kva_z_s, PGSIZE);
113                                 memset((void*)kva_z_s, 0, kva_z_e - kva_z_s);
114                                 filesz = ROUNDUP(filesz, PGSIZE);
115                         }
116                         /* Any extra pages are mapped anonymously... (a bit weird) */
117                         if (filesz < memsz)
118                                 if (do_mmap(p, memstart + filesz + pgoffset*PGSIZE, memsz-filesz,
119                                            PROT_READ|PROT_WRITE|PROT_EXEC, MAP_FIXED|MAP_ANON,
120                                            NULL, 0) == MAP_FAILED)
121                                         goto fail;
122                 }
123         }
124         /* map in program headers anyway if not present in binary.
125          * useful for TLS in static programs. */
126         if (ei->phdr == -1) {
127                 void *phdr_addr = do_mmap(p, MMAP_LOWEST_VA, PGSIZE, PROT_READ, 0, f,
128                                           0);
129                 if (phdr_addr == MAP_FAILED)
130                         goto fail;
131                 ei->phdr = (long)phdr_addr + e_phoff;
132         }
133         ei->entry = elf_field(elfhdr, e_entry) + pgoffset*PGSIZE;
134         ei->phnum = e_phnum;
135         ei->elf64 = elf64;
136         ret = 0;
137 fail:
138         kfree(elf);
139         return ret;
140 }
141
142 int load_elf(struct proc* p, struct file* f)
143 {
144         elf_info_t ei, interp_ei;
145         if (load_one_elf(p, f, 0,& ei))
146                 return -1;
147
148         if (ei.dynamic) {
149                 struct file *interp = do_file_open(ei.interp, 0, 0);
150                 if (!interp)
151                         return -1;
152                 /* careful, this could conflict with the mmap from the TLS up above */
153                 int error = load_one_elf(p, interp, 2, &interp_ei);
154                 kref_put(&interp->f_kref);
155                 if (error)
156                         return -1;
157         }
158
159         // fill in auxiliary info for dynamic linker/runtime
160         elf_aux_t auxp[] = {{ELF_AUX_PHDR, ei.phdr},
161                             {ELF_AUX_PHENT, sizeof(proghdr32_t)},
162                             {ELF_AUX_PHNUM, ei.phnum},
163                             {ELF_AUX_ENTRY, ei.entry},
164                             #ifdef __sparc_v8__
165                             {ELF_AUX_HWCAP, ELF_HWCAP_SPARC_FLUSH},
166                             #endif
167                             {0, 0}};
168
169         // put auxp after argv, envp in procinfo
170         int auxp_pos = -1;
171         for (int i = 0, zeros = 0; i < PROCINFO_MAX_ARGP; i++)
172                 if (p->procinfo->argp[i] == NULL)
173                         if (++zeros == 2)
174                                 auxp_pos = i + 1;
175         if (auxp_pos == -1 ||
176             auxp_pos + sizeof(auxp) / sizeof(char*) >= PROCINFO_MAX_ARGP)
177                 return -1;
178         memcpy(p->procinfo->argp+auxp_pos,auxp,sizeof(auxp));
179
180         uintptr_t core0_entry = ei.dynamic ? interp_ei.entry : ei.entry;
181         proc_init_trapframe(&p->env_tf,0,core0_entry,USTACKTOP);
182         p->env_entry = ei.entry;
183
184         // map in stack using POPULATE (because SPARC requires it)
185         uintptr_t stacksz = USTACK_NUM_PAGES*PGSIZE;
186         if (do_mmap(p, USTACKTOP-stacksz, stacksz, PROT_READ | PROT_WRITE,
187                     MAP_FIXED | MAP_ANONYMOUS | MAP_POPULATE, NULL, 0) == MAP_FAILED)
188                 return -1;
189
190         // Set the heap bottom and top to just past where the text 
191         // region has been loaded
192         p->heap_top = (void*)ei.highest_addr;
193         p->procinfo->heap_bottom = p->heap_top;
194
195         return 0;
196 }
197