Subversion Repositories HelenOS

Rev

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

Rev 2471 Rev 2475
1
/*
1
/*
2
 * Copyright (c) 2007 Michal Konopa
2
 * Copyright (c) 2007 Michal Konopa
3
 * Copyright (c) 2007 Martin Jelen
3
 * Copyright (c) 2007 Martin Jelen
4
 * Copyright (c) 2007 Peter Majer
4
 * Copyright (c) 2007 Peter Majer
-
 
5
 * Copyright (c) 2007 Jakub Jermar
5
 * All rights reserved.
6
 * All rights reserved.
6
 *
7
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * are met:
10
 *
11
 *
11
 * - Redistributions of source code must retain the above copyright
12
 * - Redistributions of source code must retain the above copyright
12
 *   notice, this list of conditions and the following disclaimer.
13
 *   notice, this list of conditions and the following disclaimer.
13
 * - Redistributions in binary form must reproduce the above copyright
14
 * - Redistributions in binary form must reproduce the above copyright
14
 *   notice, this list of conditions and the following disclaimer in the
15
 *   notice, this list of conditions and the following disclaimer in the
15
 *   documentation and/or other materials provided with the distribution.
16
 *   documentation and/or other materials provided with the distribution.
16
 * - The name of the author may not be used to endorse or promote products
17
 * - The name of the author may not be used to endorse or promote products
17
 *   derived from this software without specific prior written permission.
18
 *   derived from this software without specific prior written permission.
18
 *
19
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
 */
30
 
31
 
31
/** @addtogroup rd
32
/** @addtogroup rd
32
 * @{
33
 * @{
33
 */
34
 */
34
 
35
 
35
/**
36
/**
36
 * @file    rd.c
37
 * @file    rd.c
37
 * @brief   Initial RAM disk for HelenOS.
38
 * @brief   Initial RAM disk for HelenOS.
38
 */
39
 */
39
 
40
 
40
#include <ipc/ipc.h>
41
#include <ipc/ipc.h>
41
#include <ipc/services.h>
42
#include <ipc/services.h>
42
#include <ipc/ns.h>
43
#include <ipc/ns.h>
43
#include <sysinfo.h>
44
#include <sysinfo.h>
44
#include <as.h>
45
#include <as.h>
45
#include <ddi.h>
46
#include <ddi.h>
46
#include <align.h>
47
#include <align.h>
47
#include <bool.h>
48
#include <bool.h>
48
#include <errno.h>
49
#include <errno.h>
49
#include <async.h>
50
#include <async.h>
50
#include <align.h>
51
#include <align.h>
51
#include <async.h>
52
#include <async.h>
-
 
53
#include <futex.h>
52
#include "rd.h"
54
#include "rd.h"
53
 
55
 
-
 
56
/** Pointer to the ramdisk's image. */
54
static void *rd_addr;
57
static void *rd_addr;
55
static void *fs_addr;
-
 
56
 
58
 
-
 
59
/**
-
 
60
 * This futex protects the ramdisk's data.
-
 
61
 * If we were to serve multiple requests (read + write or several writes)
-
 
62
 * concurrently (i.e. from two or more threads), each read and write needs to be
-
 
63
 * protected by this futex.
-
 
64
 */
-
 
65
atomic_t rd_futex = FUTEX_INITIALIZER;
-
 
66
 
-
 
67
/** Handle one connection to ramdisk.
-
 
68
 *
-
 
69
 * @param iid       Hash of the request that opened the connection.
-
 
70
 * @param icall     Call data of the request that opened the connection.
-
 
71
 */
57
static void rd_connection(ipc_callid_t iid, ipc_call_t *icall)
72
static void rd_connection(ipc_callid_t iid, ipc_call_t *icall)
58
{
73
{
59
    ipc_callid_t callid;
74
    ipc_callid_t callid;
60
    ipc_call_t call;
75
    ipc_call_t call;
61
    int retval;
76
    int retval;
62
 
-
 
63
    ipc_answer_fast(iid, 0, 0, 0);
77
    uintptr_t fs_va = NULL;
64
    ipcarg_t offset;
78
    ipcarg_t offset;
65
 
79
 
-
 
80
    /*
-
 
81
     * We allocate VA for communication per connection.
-
 
82
     * This allows us to potentionally have more clients and work
-
 
83
     * concurrently.
-
 
84
     */
-
 
85
    fs_va = as_get_mappable_page(ALIGN_UP(BLOCK_SIZE, PAGE_SIZE));
-
 
86
    if (!fs_va) {
-
 
87
        /*
-
 
88
         * Hang up the phone if we cannot proceed any further.
-
 
89
         * This is the answer to the call that opened the connection.
-
 
90
         */
-
 
91
        ipc_answer_fast(iid, EHANGUP, 0, 0);
-
 
92
        return;
-
 
93
    } else {
-
 
94
        /*
-
 
95
         * Answer the first IPC_M_CONNECT_ME_TO call.
-
 
96
         * Return supported block size as ARG1.
-
 
97
         */
-
 
98
        ipc_answer_fast(iid, EOK, BLOCK_SIZE, 0);
-
 
99
    }
-
 
100
 
-
 
101
    /*
-
 
102
     * Now we wait for the client to send us its communication as_area.
-
 
103
     */
-
 
104
    callid = async_get_call(&call);
-
 
105
    if (IPC_GET_METHOD(call) == IPC_M_AS_AREA_SEND) {
-
 
106
        if (IPC_GET_ARG1(call) >= (ipcarg_t) BLOCK_SIZE) {
-
 
107
            /*
-
 
108
             * The client sends an as_area that can absorb the whole
-
 
109
             * block.
-
 
110
             */
-
 
111
            ipc_answer_fast(callid, EOK, (uintptr_t) fs_va, 0);
-
 
112
        } else {
-
 
113
            /*
-
 
114
             * The client offered as_area too small.
-
 
115
             * Close the connection.
-
 
116
             */
-
 
117
            ipc_answer_fast(callid, EHANGUP, 0, 0);
-
 
118
            return;    
-
 
119
        }
-
 
120
    } else {
-
 
121
        /*
-
 
122
         * The client doesn't speak the same protocol.
-
 
123
         * At this point we can't handle protocol variations.
-
 
124
         * Close the connection.
-
 
125
         */
-
 
126
        ipc_answer_fast(callid, EHANGUP, 0, 0);
-
 
127
        return;
-
 
128
    }
-
 
129
   
66
    while (1) {
130
    while (1) {
67
        callid = async_get_call(&call);
131
        callid = async_get_call(&call);
68
        switch (IPC_GET_METHOD(call)) {
132
        switch (IPC_GET_METHOD(call)) {
69
        case IPC_M_PHONE_HUNGUP:
133
        case IPC_M_PHONE_HUNGUP:
-
 
134
            /*
-
 
135
             * The other side has hung up.
-
 
136
             * Answer the message and exit the pseudo thread.
-
 
137
             */
70
            ipc_answer_fast(callid, 0, 0, 0);
138
            ipc_answer_fast(callid, EOK, 0, 0);
71
            return;
139
            return;
72
        case IPC_M_AS_AREA_SEND:
140
        case RD_READ_BLOCK:
-
 
141
            offset = IPC_GET_ARG1(call);
-
 
142
            futex_down(&rd_futex);
73
            ipc_answer_fast(callid, 0, (uintptr_t) fs_addr, 0);
143
            memcpy((void *) fs_va, rd_addr + offset, BLOCK_SIZE);
-
 
144
            futex_up(&rd_futex);
-
 
145
            retval = EOK;
74
            continue;
146
            break;
75
        case RD_READ_BLOCK:        
147
        case RD_WRITE_BLOCK:
76
            offset = IPC_GET_ARG1(call);
148
            offset = IPC_GET_ARG1(call);
-
 
149
            futex_up(&rd_futex);
77
            memcpy((void *) fs_addr, rd_addr + offset, BLOCK_SIZE);
150
            memcpy(rd_addr + offset, (void *) fs_va, BLOCK_SIZE);
-
 
151
            futex_down(&rd_futex);
78
            retval = EOK;
152
            retval = EOK;
79
            break;
153
            break;
80
        default:
154
        default:
-
 
155
            /*
-
 
156
             * The client doesn't speak the same protocol.
-
 
157
             * Instead of closing the connection, we just ignore
-
 
158
             * the call. This can be useful if the client uses a
-
 
159
             * newer version of the protocol.
-
 
160
             */
81
            retval = EINVAL;
161
            retval = EINVAL;
-
 
162
            break;
82
        }
163
        }
83
        ipc_answer_fast(callid, retval, 0, 0);
164
        ipc_answer_fast(callid, retval, 0, 0);
84
    }
165
    }
85
}
166
}
86
 
167
 
87
 
-
 
-
 
168
/** Prepare the ramdisk image for operation. */
88
static bool rd_init(void)
169
static bool rd_init(void)
89
{
170
{
90
    int retval, flags;
171
    int retval, flags;
91
 
172
 
92
    size_t rd_size = sysinfo_value("rd.size");
173
    size_t rd_size = sysinfo_value("rd.size");
93
    void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
174
    void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
94
   
175
   
95
    if (rd_size == 0)
176
    if (rd_size == 0)
96
        return false;
177
        return false;
97
   
178
   
98
    rd_addr = as_get_mappable_page(rd_size);
179
    rd_addr = as_get_mappable_page(rd_size);
99
   
180
   
100
    flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
181
    flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
101
    retval = physmem_map(rd_ph_addr, rd_addr,
182
    retval = physmem_map(rd_ph_addr, rd_addr,
102
        ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
183
        ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
103
 
184
 
104
    if (retval < 0)
185
    if (retval < 0)
105
        return false;
186
        return false;
106
   
-
 
107
    size_t fs_size = ALIGN_UP(BLOCK_SIZE * sizeof(char), PAGE_SIZE);
-
 
108
    fs_addr = as_get_mappable_page(fs_size);
-
 
109
 
-
 
110
    return true;
187
    return true;
111
}
188
}
112
 
189
 
113
int main(int argc, char **argv)
190
int main(int argc, char **argv)
114
{
191
{
115
    if (rd_init()) {
192
    if (rd_init()) {
116
        ipcarg_t phonead;
193
        ipcarg_t phonead;
117
       
194
       
118
        async_set_client_connection(rd_connection);
195
        async_set_client_connection(rd_connection);
119
       
196
       
120
        /* Register service at nameserver */
197
        /* Register service at nameserver */
121
        if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, &phonead) != 0)
198
        if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, &phonead) != 0)
122
            return -1;
199
            return -1;
123
       
200
       
124
        async_manager();
201
        async_manager();
125
       
202
       
126
        /* Never reached */
203
        /* Never reached */
127
        return 0;
204
        return 0;
128
    }
205
    }
129
   
206
   
130
    return -1;
207
    return -1;
131
}
208
}
132
 
209
 
133
/**
210
/**
134
 * @}
211
 * @}
135
 */
212
 */
136
 
213