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