Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/proc/program.c

    r91001e2 rf4876df  
    3434/**
    3535 * @file
    36  * @brief Running userspace programs.
     36 * @brief       Running userspace programs.
    3737 */
    3838
     
    6666/** Create a program using an existing address space.
    6767 *
    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 {
     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 p             Buffer for storing program information.
     72 */
     73void program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *p)
     74{
     75        as_area_t *a;
    7876        uspace_arg_t *kernel_uarg;
    79        
     77
    8078        kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
    8179        kernel_uarg->uspace_entry = (void *) entry_addr;
     
    8583        kernel_uarg->uspace_uarg = NULL;
    8684       
    87         prg->task = task_create(as, name);
    88         if (!prg->task)
    89                 return ELIMIT;
    90        
     85        p->task = task_create(as, name);
     86        ASSERT(p->task);
     87
    9188        /*
    92          * Create the data address space area.
     89         * Create the data as_area.
    9390         */
    94         as_area_t *area = as_area_create(as,
    95             AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
     91        a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
    9692            LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
    9793            AS_AREA_ATTR_NONE, &anon_backend, NULL);
    98         if (!area)
    99                 return ENOMEM;
    100        
     94
    10195        /*
    10296         * Create the main thread.
    10397         */
    104         prg->main_thread = thread_create(uinit, kernel_uarg, prg->task,
     98        p->main_thread = thread_create(uinit, kernel_uarg, p->task,
    10599            THREAD_FLAG_USPACE, "uinit", false);
    106         if (!prg->main_thread)
    107                 return ELIMIT;
    108        
    109         return EOK;
     100        ASSERT(p->main_thread);
    110101}
    111102
     
    116107 * executable image. The task is returned in *task.
    117108 *
    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.
     109 * @param image_addr    Address of an executable program image.
     110 * @param name          Name to set for the program's task.
     111 * @param p             Buffer for storing program info. If image_addr
     112 *                      points to a loader image, p->task will be set to
     113 *                      NULL and EOK will be returned.
    123114 *
    124115 * @return EOK on success or negative error code.
    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);
     116 */
     117int program_create_from_image(void *image_addr, char *name, program_t *p)
     118{
     119        as_t *as;
     120        unsigned int rc;
     121
     122        as = as_create(0);
     123        ASSERT(as);
     124
     125        rc = elf_load((elf_header_t *) image_addr, as, 0);
    134126        if (rc != EE_OK) {
    135127                as_destroy(as);
    136                 prg->task = NULL;
    137                 prg->main_thread = NULL;
    138                
     128                p->task = NULL;
     129                p->main_thread = NULL;
    139130                if (rc != EE_LOADER)
    140131                        return ENOTSUP;
    141132               
    142133                /* Register image as the program loader */
    143                 if (program_loader != NULL)
    144                         return ELIMIT;
    145                
     134                ASSERT(program_loader == NULL);
    146135                program_loader = image_addr;
    147136                LOG("Registered program loader at 0x%" PRIp "\n",
    148137                    image_addr);
    149                
    150138                return EOK;
    151139        }
    152        
    153         return program_create(as, ((elf_header_t *) image_addr)->e_entry,
    154             name, prg);
     140
     141        program_create(as, ((elf_header_t *) image_addr)->e_entry, name, p);
     142
     143        return EOK;
    155144}
    156145
    157146/** Create a task from the program loader image.
    158147 *
    159  * @param prg  Buffer for storing program info.
    160  * @param name Name to set for the program's task.
     148 * @param p     Buffer for storing program info.
     149 * @param name  Name to set for the program's task.
    161150 *
    162151 * @return EOK on success or negative error code.
    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;
     152 */
     153int program_create_loader(program_t *p, char *name)
     154{
     155        as_t *as;
     156        unsigned int rc;
     157        void *loader;
     158
     159        as = as_create(0);
     160        ASSERT(as);
     161
     162        loader = program_loader;
    172163        if (!loader) {
    173164                printf("Cannot spawn loader as none was registered\n");
    174165                return ENOENT;
    175166        }
    176        
    177         unsigned int rc = elf_load((elf_header_t *) program_loader, as,
    178             ELD_F_LOADER);
     167
     168        rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER);
    179169        if (rc != EE_OK) {
    180170                as_destroy(as);
    181171                return ENOENT;
    182172        }
    183        
    184         return program_create(as, ((elf_header_t *) program_loader)->e_entry,
    185             name, prg);
     173
     174        program_create(as, ((elf_header_t *) program_loader)->e_entry,
     175            name, p);
     176
     177        return EOK;
    186178}
    187179
     
    190182 * Switch program's main thread to the ready state.
    191183 *
    192  * @param prg Program to make ready.
    193  *
    194  */
    195 void program_ready(program_t *prg)
    196 {
    197         thread_ready(prg->main_thread);
     184 * @param p Program to make ready.
     185 */
     186void program_ready(program_t *p)
     187{
     188        thread_ready(p->main_thread);
    198189}
    199190
     
    203194 * the task name.
    204195 *
    205  * @param uspace_name Name to set on the new task (typically the same
    206  *                    as the command used to execute it).
    207  * @param name_len    Length of the name.
    208  *
    209  * @return EOK on success or an error code from @ref errno.h.
    210  *
     196 * @param name                  Name to set on the new task (typically the same
     197 *                              as the command used to execute it).
     198 *
     199 * @return 0 on success or an error code from @ref errno.h.
    211200 */
    212201unative_t sys_program_spawn_loader(char *uspace_name, size_t name_len)
    213202{
     203        program_t p;
     204        int rc;
     205        char namebuf[TASK_NAME_BUFLEN];
     206
    214207        /* Cap length of name and copy it from userspace. */
     208
    215209        if (name_len > TASK_NAME_BUFLEN - 1)
    216210                name_len = TASK_NAME_BUFLEN - 1;
    217        
    218         char namebuf[TASK_NAME_BUFLEN];
    219         int rc = copy_from_uspace(namebuf, uspace_name, name_len);
     211
     212        rc = copy_from_uspace(namebuf, uspace_name, name_len);
    220213        if (rc != 0)
    221214                return (unative_t) rc;
    222        
     215
    223216        namebuf[name_len] = 0;
    224        
     217
    225218        /* Spawn the new task. */
    226         program_t prg;
    227         rc = program_create_loader(&prg, namebuf);
     219
     220        rc = program_create_loader(&p, namebuf);
    228221        if (rc != 0)
    229222                return rc;
    230        
     223
    231224        // FIXME: control the capabilities
    232         cap_set(prg.task, cap_get(TASK));
    233         program_ready(&prg);
    234        
     225        cap_set(p.task, cap_get(TASK));
     226
     227        program_ready(&p);
     228
    235229        return EOK;
    236230}
Note: See TracChangeset for help on using the changeset viewer.