Subversion Repositories HelenOS

Rev

Rev 3204 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3203 svoboda 1
/*
2
 * Copyright (c) 2001-2004 Jakub Jermar
3
 * Copyright (c) 2008 Jiri Svoboda
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
/** @addtogroup genericproc
31
 * @{
32
 */
33
 
34
/**
35
 * @file
36
 * @brief   Running userspace programs.
37
 */
38
 
39
#include <main/uinit.h>
40
#include <proc/thread.h>
41
#include <proc/task.h>
42
#include <proc/uarg.h>
43
#include <mm/as.h>
44
#include <mm/slab.h>
45
#include <arch.h>
46
#include <adt/list.h>
47
#include <ipc/ipc.h>
48
#include <ipc/ipcrsc.h>
49
#include <security/cap.h>
50
#include <lib/elf.h>
51
#include <errno.h>
52
#include <syscall/copy.h>
53
#include <proc/program.h>
54
 
55
#ifndef LOADED_PROG_STACK_PAGES_NO
56
#define LOADED_PROG_STACK_PAGES_NO 1
57
#endif
58
 
59
/**
60
 * Points to the binary image used as the program loader. All non-initial
61
 * tasks are created from this executable image.
62
 */
63
void *program_loader = NULL;
64
 
65
/** Create new task with 1 thread and run it
66
 *
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 Program name.
70
 *
71
 * @return Task of the running program or NULL on error.
72
 */
73
void program_create(as_t *as, uintptr_t entry_addr, program_t *p)
74
{
75
    as_area_t *a;
76
    uspace_arg_t *kernel_uarg;
77
 
78
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
79
    kernel_uarg->uspace_entry = (void *) entry_addr;
80
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
81
    kernel_uarg->uspace_thread_function = NULL;
82
    kernel_uarg->uspace_thread_arg = NULL;
83
    kernel_uarg->uspace_uarg = NULL;
84
 
85
    p->task = task_create(as, "app");
86
    ASSERT(p->task);
87
 
88
    /*
89
     * Create the data as_area.
90
     */
91
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
92
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
93
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
94
 
95
    /*
96
     * Create the main thread.
97
     */
98
    p->main_thread = thread_create(uinit, kernel_uarg, p->task,
99
        THREAD_FLAG_USPACE, "uinit", false);
100
    ASSERT(p->main_thread);
101
}
102
 
103
/** Parse an executable image in the physical memory.
104
 *
105
 * If the image belongs to a program loader, it is registered as such,
106
 * (and *task is set to NULL). Otherwise a task is created from the
107
 * executable image. The task is returned in *task.
108
 *
109
 * @param program_addr Address of program executable image.
110
 * @param name Program name.
111
 * @param task Where to store the pointer to the newly created task.
112
 *
113
 * @return EOK on success or negative error code.
114
 */
115
int program_create_from_image(void *image_addr, program_t *p)
116
{
117
    as_t *as;
118
    unsigned int rc;
119
 
120
    as = as_create(0);
121
    ASSERT(as);
122
 
123
    rc = elf_load((elf_header_t *) image_addr, as, 0);
124
    if (rc != EE_OK) {
125
        as_destroy(as);
126
        p->task = NULL;
127
        p->main_thread = NULL;
128
        if (rc != EE_LOADER)
129
            return ENOTSUP;
130
 
131
        /* Register image as the program loader */
132
        ASSERT(program_loader == NULL);
133
        program_loader = image_addr;
134
        return EOK;
135
    }
136
 
137
    program_create(as, ((elf_header_t *) image_addr)->e_entry, p);
138
 
139
    return EOK;
140
}
141
 
142
/** Create a task from the program loader image.
143
 *
144
 * @param name Program name.
145
 * @param t Buffer for storing pointer to the newly created task.
146
 *
147
 * @return Task of the running program or NULL on error.
148
 */
149
int program_create_loader(program_t *p)
150
{
151
    as_t *as;
152
    unsigned int rc;
153
    void *loader;
154
 
155
    as = as_create(0);
156
    ASSERT(as);
157
 
158
    loader = program_loader;
159
    if (!loader) return ENOENT;
160
 
161
    rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER);
162
    if (rc != EE_OK) {
163
        as_destroy(as);
164
        return ENOENT;
165
    }
166
 
167
    program_create(as, ((elf_header_t *) program_loader)->e_entry, p);
168
 
169
    return EOK;
170
}
171
 
172
/** Make task ready.
173
 *
174
 * Switch task's thread to the ready state.
175
 *
176
 * @param ta Task to make ready.
177
 */
178
void program_ready(program_t *p)
179
{
180
    thread_ready(p->main_thread);
181
}
182
 
183
/** Syscall for creating a new task from userspace.
184
 *
185
 * Creates a new task from the program loader image, connects a phone
186
 * to it and stores the phone id into the provided buffer.
187
 *
188
 * @param uspace_phone_id Userspace address where to store the phone id.
189
 *
190
 * @return 0 on success or an error code from @ref errno.h.
191
 */
192
unative_t sys_program_spawn_loader(int *uspace_phone_id)
193
{
194
    program_t p;
195
    int fake_id;
196
    int rc;
197
    int phone_id;
198
 
199
    fake_id = 0;
200
 
201
    /* Before we even try creating the task, see if we can write the id */
202
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id,
203
        sizeof(fake_id));
204
    if (rc != 0)
205
        return rc;
206
 
207
    phone_id = phone_alloc();
208
    if (phone_id < 0)
209
        return ELIMIT;
210
 
211
    rc = program_create_loader(&p);
212
    if (rc != 0)
213
        return rc;
214
 
215
    phone_connect(phone_id, &p.task->answerbox);
216
 
217
    /* No need to aquire lock before task_ready() */
218
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id,
219
        sizeof(phone_id));
220
    if (rc != 0) {
221
        /* Ooops */
222
        ipc_phone_hangup(&TASK->phones[phone_id]);
223
        task_kill(p.task->taskid);
224
        return rc;
225
    }
226
 
227
    // FIXME: control the capabilities
228
    cap_set(p.task, cap_get(TASK));
229
 
230
    program_ready(&p);
231
 
232
    return EOK;
233
}
234
 
235
/** @}
236
 */