Subversion Repositories HelenOS

Rev

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

Rev 3343 Rev 3593
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 <print.h>
52
#include <print.h>
53
#include <syscall/copy.h>
53
#include <syscall/copy.h>
54
#include <proc/program.h>
54
#include <proc/program.h>
55
 
55
 
56
#ifndef LOADED_PROG_STACK_PAGES_NO
56
#ifndef LOADED_PROG_STACK_PAGES_NO
57
#define LOADED_PROG_STACK_PAGES_NO 1
57
#define LOADED_PROG_STACK_PAGES_NO 1
58
#endif
58
#endif
59
 
59
 
60
/**
60
/**
61
 * Points to the binary image used as the program loader. All non-initial
61
 * Points to the binary image used as the program loader. All non-initial
62
 * tasks are created from this executable image.
62
 * tasks are created from this executable image.
63
 */
63
 */
64
void *program_loader = NULL;
64
void *program_loader = NULL;
65
 
65
 
66
/** Create a program using an existing address space.
66
/** Create a program using an existing address space.
67
 *
67
 *
68
 * @param as        Address space containing a binary program image.
68
 * @param as        Address space containing a binary program image.
69
 * @param entry_addr    Program entry-point address in program address space.
69
 * @param entry_addr    Program entry-point address in program address space.
-
 
70
 * @param name      Name to set for the program's task.
70
 * @param p     Buffer for storing program information.
71
 * @param p     Buffer for storing program information.
71
 */
72
 */
72
void program_create(as_t *as, uintptr_t entry_addr, program_t *p)
73
void program_create(as_t *as, uintptr_t entry_addr, char *name, program_t *p)
73
{
74
{
74
    as_area_t *a;
75
    as_area_t *a;
75
    uspace_arg_t *kernel_uarg;
76
    uspace_arg_t *kernel_uarg;
76
 
77
 
77
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
78
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
78
    kernel_uarg->uspace_entry = (void *) entry_addr;
79
    kernel_uarg->uspace_entry = (void *) entry_addr;
79
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
80
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
80
    kernel_uarg->uspace_thread_function = NULL;
81
    kernel_uarg->uspace_thread_function = NULL;
81
    kernel_uarg->uspace_thread_arg = NULL;
82
    kernel_uarg->uspace_thread_arg = NULL;
82
    kernel_uarg->uspace_uarg = NULL;
83
    kernel_uarg->uspace_uarg = NULL;
83
   
84
   
84
    p->task = task_create(as, "app");
85
    p->task = task_create(as, name);
85
    ASSERT(p->task);
86
    ASSERT(p->task);
86
 
87
 
87
    /*
88
    /*
88
     * Create the data as_area.
89
     * Create the data as_area.
89
     */
90
     */
90
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
91
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
91
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
92
        LOADED_PROG_STACK_PAGES_NO * PAGE_SIZE, USTACK_ADDRESS,
92
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
93
        AS_AREA_ATTR_NONE, &anon_backend, NULL);
93
 
94
 
94
    /*
95
    /*
95
     * Create the main thread.
96
     * Create the main thread.
96
     */
97
     */
97
    p->main_thread = thread_create(uinit, kernel_uarg, p->task,
98
    p->main_thread = thread_create(uinit, kernel_uarg, p->task,
98
        THREAD_FLAG_USPACE, "uinit", false);
99
        THREAD_FLAG_USPACE, "uinit", false);
99
    ASSERT(p->main_thread);
100
    ASSERT(p->main_thread);
100
}
101
}
101
 
102
 
102
/** Parse an executable image in the kernel memory.
103
/** Parse an executable image in the kernel memory.
103
 *
104
 *
104
 * If the image belongs to a program loader, it is registered as such,
105
 * If the image belongs to a program loader, it is registered as such,
105
 * (and *task is set to NULL). Otherwise a task is created from the
106
 * (and *task is set to NULL). Otherwise a task is created from the
106
 * executable image. The task is returned in *task.
107
 * executable image. The task is returned in *task.
107
 *
108
 *
108
 * @param image_addr    Address of an executable program image.
109
 * @param image_addr    Address of an executable program image.
-
 
110
 * @param name      Name to set for the program's task.
109
 * @param p     Buffer for storing program info. If image_addr
111
 * @param p     Buffer for storing program info. If image_addr
110
 *          points to a loader image, p->task will be set to
112
 *          points to a loader image, p->task will be set to
111
 *          NULL and EOK will be returned.
113
 *          NULL and EOK will be returned.
112
 *
114
 *
113
 * @return EOK on success or negative error code.
115
 * @return EOK on success or negative error code.
114
 */
116
 */
115
int program_create_from_image(void *image_addr, program_t *p)
117
int program_create_from_image(void *image_addr, char *name, program_t *p)
116
{
118
{
117
    as_t *as;
119
    as_t *as;
118
    unsigned int rc;
120
    unsigned int rc;
119
 
121
 
120
    as = as_create(0);
122
    as = as_create(0);
121
    ASSERT(as);
123
    ASSERT(as);
122
 
124
 
123
    rc = elf_load((elf_header_t *) image_addr, as, 0);
125
    rc = elf_load((elf_header_t *) image_addr, as, 0);
124
    if (rc != EE_OK) {
126
    if (rc != EE_OK) {
125
        as_destroy(as);
127
        as_destroy(as);
126
        p->task = NULL;
128
        p->task = NULL;
127
        p->main_thread = NULL;
129
        p->main_thread = NULL;
128
        if (rc != EE_LOADER)
130
        if (rc != EE_LOADER)
129
            return ENOTSUP;
131
            return ENOTSUP;
130
       
132
       
131
        /* Register image as the program loader */
133
        /* Register image as the program loader */
132
        ASSERT(program_loader == NULL);
134
        ASSERT(program_loader == NULL);
133
        program_loader = image_addr;
135
        program_loader = image_addr;
134
        printf("Registered program loader at 0x%" PRIp "\n",
136
        printf("Registered program loader at 0x%" PRIp "\n",
135
            image_addr);
137
            image_addr);
136
        return EOK;
138
        return EOK;
137
    }
139
    }
138
 
140
 
139
    program_create(as, ((elf_header_t *) image_addr)->e_entry, p);
141
    program_create(as, ((elf_header_t *) image_addr)->e_entry, name, p);
140
 
142
 
141
    return EOK;
143
    return EOK;
142
}
144
}
143
 
145
 
144
/** Create a task from the program loader image.
146
/** Create a task from the program loader image.
145
 *
147
 *
146
 * @param p Buffer for storing program info.
148
 * @param p Buffer for storing program info.
-
 
149
 * @param name  Name to set for the program's task.
-
 
150
 *
147
 * @return EOK on success or negative error code.
151
 * @return EOK on success or negative error code.
148
 */
152
 */
149
int program_create_loader(program_t *p)
153
int program_create_loader(program_t *p, char *name)
150
{
154
{
151
    as_t *as;
155
    as_t *as;
152
    unsigned int rc;
156
    unsigned int rc;
153
    void *loader;
157
    void *loader;
154
 
158
 
155
    as = as_create(0);
159
    as = as_create(0);
156
    ASSERT(as);
160
    ASSERT(as);
157
 
161
 
158
    loader = program_loader;
162
    loader = program_loader;
159
    if (!loader) {
163
    if (!loader) {
160
        printf("Cannot spawn loader as none was registered\n");
164
        printf("Cannot spawn loader as none was registered\n");
161
        return ENOENT;
165
        return ENOENT;
162
    }
166
    }
163
 
167
 
164
    rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER);
168
    rc = elf_load((elf_header_t *) program_loader, as, ELD_F_LOADER);
165
    if (rc != EE_OK) {
169
    if (rc != EE_OK) {
166
        as_destroy(as);
170
        as_destroy(as);
167
        return ENOENT;
171
        return ENOENT;
168
    }
172
    }
169
 
173
 
170
    program_create(as, ((elf_header_t *) program_loader)->e_entry, p);
174
    program_create(as, ((elf_header_t *) program_loader)->e_entry,
-
 
175
        name, p);
171
 
176
 
172
    return EOK;
177
    return EOK;
173
}
178
}
174
 
179
 
175
/** Make program ready.
180
/** Make program ready.
176
 *
181
 *
177
 * Switch program's main thread to the ready state.
182
 * Switch program's main thread to the ready state.
178
 *
183
 *
179
 * @param p Program to make ready.
184
 * @param p Program to make ready.
180
 */
185
 */
181
void program_ready(program_t *p)
186
void program_ready(program_t *p)
182
{
187
{
183
    thread_ready(p->main_thread);
188
    thread_ready(p->main_thread);
184
}
189
}
185
 
190
 
186
/** Syscall for creating a new loader instance from userspace.
191
/** Syscall for creating a new loader instance from userspace.
187
 *
192
 *
188
 * Creates a new task from the program loader image, connects a phone
193
 * Creates a new task from the program loader image, connects a phone
189
 * to it and stores the phone id into the provided buffer.
194
 * to it and stores the phone id into the provided buffer.
190
 *
195
 *
191
 * @param uspace_phone_id Userspace address where to store the phone id.
196
 * @param uspace_phone_id   Userspace address where to store the phone id.
-
 
197
 * @param name          Name to set on the new task (typically the same
-
 
198
 *              as the command used to execute it).
192
 *
199
 *
193
 * @return 0 on success or an error code from @ref errno.h.
200
 * @return 0 on success or an error code from @ref errno.h.
194
 */
201
 */
195
unative_t sys_program_spawn_loader(int *uspace_phone_id)
202
unative_t sys_program_spawn_loader(int *uspace_phone_id, char *uspace_name,
-
 
203
    size_t name_len)
196
{
204
{
197
    program_t p;
205
    program_t p;
198
    int fake_id;
206
    int fake_id;
199
    int rc;
207
    int rc;
200
    int phone_id;
208
    int phone_id;
-
 
209
    char namebuf[TASK_NAME_BUFLEN];
201
 
210
 
202
    fake_id = 0;
211
    fake_id = 0;
203
 
212
 
204
    /* Before we even try creating the task, see if we can write the id */
213
    /* Before we even try creating the task, see if we can write the id */
205
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id,
214
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &fake_id,
206
        sizeof(fake_id));
215
        sizeof(fake_id));
207
    if (rc != 0)
216
    if (rc != 0)
208
        return rc;
217
        return rc;
209
 
218
 
-
 
219
    /* Cap length of name and copy it from userspace. */
-
 
220
 
-
 
221
    if (name_len > THREAD_NAME_BUFLEN - 1)
-
 
222
        name_len = THREAD_NAME_BUFLEN - 1;
-
 
223
 
-
 
224
    rc = copy_from_uspace(namebuf, uspace_name, name_len);
-
 
225
    if (rc != 0)
-
 
226
        return (unative_t) rc;
-
 
227
 
-
 
228
    namebuf[name_len] = '\0';
-
 
229
 
-
 
230
    /* Allocate the phone for communicating with the new task. */
-
 
231
 
210
    phone_id = phone_alloc();
232
    phone_id = phone_alloc();
211
    if (phone_id < 0)
233
    if (phone_id < 0)
212
        return ELIMIT;
234
        return ELIMIT;
213
 
235
 
-
 
236
    /* Spawn the new task. */
-
 
237
 
214
    rc = program_create_loader(&p);
238
    rc = program_create_loader(&p, namebuf);
215
    if (rc != 0)
239
    if (rc != 0)
216
        return rc;
240
        return rc;
217
 
241
 
218
    phone_connect(phone_id, &p.task->answerbox);
242
    phone_connect(phone_id, &p.task->answerbox);
219
 
243
 
220
    /* No need to aquire lock before task_ready() */
244
    /* No need to aquire lock before task_ready() */
221
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id,
245
    rc = (unative_t) copy_to_uspace(uspace_phone_id, &phone_id,
222
        sizeof(phone_id));
246
        sizeof(phone_id));
223
    if (rc != 0) {
247
    if (rc != 0) {
224
        /* Ooops */
248
        /* Ooops */
225
        ipc_phone_hangup(&TASK->phones[phone_id]);
249
        ipc_phone_hangup(&TASK->phones[phone_id]);
226
        task_kill(p.task->taskid);
250
        task_kill(p.task->taskid);
227
        return rc;
251
        return rc;
228
    }
252
    }
229
 
253
 
230
    // FIXME: control the capabilities
254
    // FIXME: control the capabilities
231
    cap_set(p.task, cap_get(TASK));
255
    cap_set(p.task, cap_get(TASK));
232
 
256
 
233
    program_ready(&p);
257
    program_ready(&p);
234
 
258
 
235
    return EOK;
259
    return EOK;
236
}
260
}
237
 
261
 
238
/** @}
262
/** @}
239
 */
263
 */
240
 
264