Subversion Repositories HelenOS

Rev

Rev 3203 | Rev 3569 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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