Changes in kernel/generic/src/proc/program.c [1ea99cc:bfe43d5] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/proc/program.c
r1ea99cc rbfe43d5 34 34 /** 35 35 * @file 36 * @brief 36 * @brief Running userspace programs. 37 37 */ 38 38 … … 50 50 #include <lib/elf.h> 51 51 #include <errno.h> 52 #include <print.h> 52 53 #include <syscall/copy.h> 53 54 #include <proc/program.h> … … 65 66 /** Create a program using an existing address space. 66 67 * 67 * @param as Address space containing a binary program image. 68 * @param entry_addr Program entry-point address in program address space. 69 * @param name Name to set for the program's task. 70 * @param p Buffer for storing program information. 71 */ 72 void program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *p) 73 { 74 as_area_t *a; 68 * @param as Address space containing a binary program image. 69 * @param entry_addr Program entry-point address in program address space. 70 * @param name Name to set for the program's task. 71 * @param prg Buffer for storing program information. 72 * 73 * @return EOK on success or negative error code. 74 * 75 */ 76 int program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *prg) 77 { 75 78 uspace_arg_t *kernel_uarg; 76 79 77 80 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); 78 81 kernel_uarg->uspace_entry = (void *) entry_addr; … … 82 85 kernel_uarg->uspace_uarg = NULL; 83 86 84 p->task = task_create(as, name); 85 ASSERT(p->task); 86 87 prg->task = task_create(as, name); 88 if (!prg->task) 89 return ELIMIT; 90 87 91 /* 88 * Create the data a s_area.92 * Create the data address space area. 89 93 */ 90 a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, 94 as_area_t *area = as_area_create(as, 95 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, 91 96 LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS, 92 97 AS_AREA_ATTR_NONE, &anon_backend, NULL); 93 98 if (!area) 99 return ENOMEM; 100 94 101 /* 95 102 * Create the main thread. 96 103 */ 97 p ->main_thread = thread_create(uinit, kernel_uarg, p->task,104 prg->main_thread = thread_create(uinit, kernel_uarg, prg->task, 98 105 THREAD_FLAG_USPACE, "uinit", false); 99 ASSERT(p->main_thread); 106 if (!prg->main_thread) 107 return ELIMIT; 108 109 return EOK; 100 110 } 101 111 … … 106 116 * executable image. The task is returned in *task. 107 117 * 108 * @param image_addr 109 * @param name 110 * @param p 111 * 112 * 118 * @param image_addr Address of an executable program image. 119 * @param name Name to set for the program's task. 120 * @param prg Buffer for storing program info. If image_addr 121 * points to a loader image, p->task will be set to 122 * NULL and EOK will be returned. 113 123 * 114 124 * @return EOK on success or negative error code. 115 */ 116 int program_create_from_image(void *image_addr, char *name, program_t *p) 117 { 118 as_t *as; 119 unsigned int rc; 120 121 as = as_create(0); 122 ASSERT(as); 123 124 rc = elf_load((elf_header_t *) image_addr, as, 0); 125 * 126 */ 127 int program_create_from_image(void *image_addr, char *name, program_t *prg) 128 { 129 as_t *as = as_create(0); 130 if (!as) 131 return ENOMEM; 132 133 unsigned int rc = elf_load((elf_header_t *) image_addr, as, 0); 125 134 if (rc != EE_OK) { 126 135 as_destroy(as); 127 p->task = NULL; 128 p->main_thread = NULL; 136 prg->task = NULL; 137 prg->main_thread = NULL; 138 129 139 if (rc != EE_LOADER) 130 140 return ENOTSUP; 131 141 132 142 /* Register image as the program loader */ 133 ASSERT(program_loader == NULL); 143 if (program_loader != NULL) 144 return ELIMIT; 145 134 146 program_loader = image_addr; 147 LOG("Registered program loader at %p", 148 (void *) image_addr); 149 135 150 return EOK; 136 151 } 137 138 program_create(as, ((elf_header_t *) image_addr)->e_entry, name, p); 139 140 return EOK; 152 153 return program_create(as, ((elf_header_t *) image_addr)->e_entry, 154 name, prg); 141 155 } 142 156 143 157 /** Create a task from the program loader image. 144 158 * 145 * @param p 146 * @param name 159 * @param prg Buffer for storing program info. 160 * @param name Name to set for the program's task. 147 161 * 148 162 * @return EOK on success or negative error code. 149 */ 150 int program_create_loader(program_t *p, char *name) 151 { 152 as_t *as; 153 unsigned int rc; 154 void *loader; 155 156 as = as_create(0); 157 ASSERT(as); 158 159 loader = program_loader; 160 if (!loader) return ENOENT; 161 162 rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER); 163 * 164 */ 165 int program_create_loader(program_t *prg, char *name) 166 { 167 as_t *as = as_create(0); 168 if (!as) 169 return ENOMEM; 170 171 void *loader = program_loader; 172 if (!loader) { 173 as_destroy(as); 174 printf("Cannot spawn loader as none was registered\n"); 175 return ENOENT; 176 } 177 178 unsigned int rc = elf_load((elf_header_t *) program_loader, as, 179 ELD_F_LOADER); 163 180 if (rc != EE_OK) { 164 181 as_destroy(as); 182 printf("Cannot spawn loader (%s)\n", elf_error(rc)); 165 183 return ENOENT; 166 184 } 167 168 program_create(as, ((elf_header_t *) program_loader)->e_entry, 169 name, p); 170 171 return EOK; 185 186 return program_create(as, ((elf_header_t *) program_loader)->e_entry, 187 name, prg); 172 188 } 173 189 … … 176 192 * Switch program's main thread to the ready state. 177 193 * 178 * @param p Program to make ready. 179 */ 180 void program_ready(program_t *p) 181 { 182 thread_ready(p->main_thread); 194 * @param prg Program to make ready. 195 * 196 */ 197 void program_ready(program_t *prg) 198 { 199 thread_ready(prg->main_thread); 183 200 } 184 201 … … 188 205 * the task name. 189 206 * 190 * @param name Name to set on the new task (typically the same 191 * as the command used to execute it). 192 * 193 * @return 0 on success or an error code from @ref errno.h. 194 */ 195 unative_t sys_program_spawn_loader(char *uspace_name, size_t name_len) 196 { 197 program_t p; 198 int rc; 199 char namebuf[TASK_NAME_BUFLEN]; 200 207 * @param uspace_name Name to set on the new task (typically the same 208 * as the command used to execute it). 209 * @param name_len Length of the name. 210 * 211 * @return EOK on success or an error code from @ref errno.h. 212 * 213 */ 214 sysarg_t sys_program_spawn_loader(char *uspace_name, size_t name_len) 215 { 201 216 /* Cap length of name and copy it from userspace. */ 202 203 217 if (name_len > TASK_NAME_BUFLEN - 1) 204 218 name_len = TASK_NAME_BUFLEN - 1; 205 206 rc = copy_from_uspace(namebuf, uspace_name, name_len); 219 220 char namebuf[TASK_NAME_BUFLEN]; 221 int rc = copy_from_uspace(namebuf, uspace_name, name_len); 207 222 if (rc != 0) 208 return ( unative_t) rc;209 223 return (sysarg_t) rc; 224 210 225 namebuf[name_len] = 0; 211 226 212 227 /* Spawn the new task. */ 213 214 rc = program_create_loader(&p , namebuf);228 program_t prg; 229 rc = program_create_loader(&prg, namebuf); 215 230 if (rc != 0) 216 231 return rc; 217 232 218 233 // FIXME: control the capabilities 219 cap_set(p.task, cap_get(TASK)); 220 221 program_ready(&p); 222 234 cap_set(prg.task, cap_get(TASK)); 235 program_ready(&prg); 236 223 237 return EOK; 224 238 }
Note:
See TracChangeset
for help on using the changeset viewer.