Subversion Repositories HelenOS

Rev

Rev 3022 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3022 Rev 4055
Line 49... Line 49...
49
#include <errno.h>
49
#include <errno.h>
50
#include <async.h>
50
#include <async.h>
51
#include <align.h>
51
#include <align.h>
52
#include <async.h>
52
#include <async.h>
53
#include <futex.h>
53
#include <futex.h>
-
 
54
#include <stdio.h>
-
 
55
#include <ipc/devmap.h>
54
#include "rd.h"
56
#include "rd.h"
55
 
57
 
-
 
58
#define NAME "rd"
-
 
59
 
56
/** Pointer to the ramdisk's image. */
60
/** Pointer to the ramdisk's image. */
57
static void *rd_addr;
61
static void *rd_addr;
58
/** Size of the ramdisk. */
62
/** Size of the ramdisk. */
59
static size_t rd_size;
63
static size_t rd_size;
60
 
64
 
Line 75... Line 79...
75
{
79
{
76
    ipc_callid_t callid;
80
    ipc_callid_t callid;
77
    ipc_call_t call;
81
    ipc_call_t call;
78
    int retval;
82
    int retval;
79
    void *fs_va = NULL;
83
    void *fs_va = NULL;
80
    ipcarg_t offset;
84
    off_t offset;
-
 
85
    size_t block_size;
-
 
86
    size_t maxblock_size;
81
 
87
 
82
    /*
88
    /*
83
     * We allocate VA for communication per connection.
89
     * Answer the first IPC_M_CONNECT_ME_TO call.
84
     * This allows us to potentionally have more clients and work
-
 
85
     * concurrently.
-
 
86
     */
90
     */
87
    fs_va = as_get_mappable_page(ALIGN_UP(BLOCK_SIZE, PAGE_SIZE));
-
 
88
    if (!fs_va) {
-
 
89
        /*
-
 
90
         * Hang up the phone if we cannot proceed any further.
-
 
91
         * This is the answer to the call that opened the connection.
-
 
92
         */
-
 
93
        ipc_answer_0(iid, EHANGUP);
91
    ipc_answer_0(iid, EOK);
94
        return;
-
 
95
    } else {
-
 
96
        /*
-
 
97
         * Answer the first IPC_M_CONNECT_ME_TO call.
-
 
98
         * Return supported block size as ARG1.
-
 
99
         */
-
 
100
        ipc_answer_1(iid, EOK, BLOCK_SIZE);
-
 
101
    }
-
 
102
 
92
 
103
    /*
93
    /*
104
     * Now we wait for the client to send us its communication as_area.
94
     * Now we wait for the client to send us its communication as_area.
105
     */
95
     */
106
    size_t size;
96
    int flags;
107
    if (ipc_share_out_receive(&callid, &size, NULL)) {
97
    if (ipc_share_out_receive(&callid, &maxblock_size, &flags)) {
108
        if (size >= BLOCK_SIZE) {
-
 
109
            /*
-
 
110
             * The client sends an as_area that can absorb the whole
98
        fs_va = as_get_mappable_page(maxblock_size);
111
             * block.
99
        if (fs_va) {
112
             */
-
 
113
            (void) ipc_share_out_finalize(callid, fs_va);
100
            (void) ipc_share_out_finalize(callid, fs_va);
114
        } else {
101
        } else {
115
            /*
-
 
116
             * The client offered as_area too small.
-
 
117
             * Close the connection.
-
 
118
             */
-
 
119
            ipc_answer_0(callid, EHANGUP);
102
            ipc_answer_0(callid, EHANGUP);
120
            return;    
103
            return;    
121
        }
104
        }
122
    } else {
105
    } else {
123
        /*
106
        /*
Line 139... Line 122...
139
             */
122
             */
140
            ipc_answer_0(callid, EOK);
123
            ipc_answer_0(callid, EOK);
141
            return;
124
            return;
142
        case RD_READ_BLOCK:
125
        case RD_READ_BLOCK:
143
            offset = IPC_GET_ARG1(call);
126
            offset = IPC_GET_ARG1(call);
-
 
127
            block_size = IPC_GET_ARG2(call);
-
 
128
            if (block_size > maxblock_size) {
-
 
129
                /*
-
 
130
                 * Maximum block size exceeded.
-
 
131
                 */
-
 
132
                retval = ELIMIT;
-
 
133
                break;
-
 
134
            }
144
            if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) {
135
            if (offset * block_size > rd_size - block_size) {
145
                /*
136
                /*
146
                 * Reading past the end of the device.
137
                 * Reading past the end of the device.
147
                 */
138
                 */
148
                retval = ELIMIT;
139
                retval = ELIMIT;
149
                break;
140
                break;
150
            }
141
            }
151
            futex_down(&rd_futex);
142
            futex_down(&rd_futex);
152
            memcpy(fs_va, rd_addr + offset, BLOCK_SIZE);
143
            memcpy(fs_va, rd_addr + offset * block_size, block_size);
153
            futex_up(&rd_futex);
144
            futex_up(&rd_futex);
154
            retval = EOK;
145
            retval = EOK;
155
            break;
146
            break;
156
        case RD_WRITE_BLOCK:
147
        case RD_WRITE_BLOCK:
157
            offset = IPC_GET_ARG1(call);
148
            offset = IPC_GET_ARG1(call);
-
 
149
            block_size = IPC_GET_ARG2(call);
-
 
150
            if (block_size > maxblock_size) {
-
 
151
                /*
-
 
152
                 * Maximum block size exceeded.
-
 
153
                 */
-
 
154
                retval = ELIMIT;
-
 
155
                break;
-
 
156
            }
158
            if (offset * BLOCK_SIZE > rd_size - BLOCK_SIZE) {
157
            if (offset * block_size > rd_size - block_size) {
159
                /*
158
                /*
160
                 * Writing past the end of the device.
159
                 * Writing past the end of the device.
161
                 */
160
                 */
162
                retval = ELIMIT;
161
                retval = ELIMIT;
163
                break;
162
                break;
164
            }
163
            }
165
            futex_up(&rd_futex);
164
            futex_up(&rd_futex);
166
            memcpy(rd_addr + offset, fs_va, BLOCK_SIZE);
165
            memcpy(rd_addr + offset * block_size, fs_va, block_size);
167
            futex_down(&rd_futex);
166
            futex_down(&rd_futex);
168
            retval = EOK;
167
            retval = EOK;
169
            break;
168
            break;
170
        default:
169
        default:
171
            /*
170
            /*
Line 179... Line 178...
179
        }
178
        }
180
        ipc_answer_0(callid, retval);
179
        ipc_answer_0(callid, retval);
181
    }
180
    }
182
}
181
}
183
 
182
 
-
 
183
static int driver_register(char *name)
-
 
184
{
-
 
185
    ipcarg_t retval;
-
 
186
    aid_t req;
-
 
187
    ipc_call_t answer;
-
 
188
    int phone;
-
 
189
    ipcarg_t callback_phonehash;
-
 
190
 
-
 
191
    phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, DEVMAP_DRIVER, 0);
-
 
192
    if (phone < 0) {
-
 
193
        printf(NAME ": Failed to connect to device mapper\n");
-
 
194
        return -1;
-
 
195
    }
-
 
196
   
-
 
197
    req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
-
 
198
 
-
 
199
    retval = ipc_data_write_start(phone, (char *) name, strlen(name) + 1);
-
 
200
 
-
 
201
    if (retval != EOK) {
-
 
202
        async_wait_for(req, NULL);
-
 
203
        return -1;
-
 
204
    }
-
 
205
 
-
 
206
    async_set_client_connection(rd_connection);
-
 
207
 
-
 
208
    ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
-
 
209
    async_wait_for(req, &retval);
-
 
210
 
-
 
211
    return phone;
-
 
212
}
-
 
213
 
-
 
214
static int device_register(int driver_phone, char *name, int *handle)
-
 
215
{
-
 
216
    ipcarg_t retval;
-
 
217
    aid_t req;
-
 
218
    ipc_call_t answer;
-
 
219
 
-
 
220
    req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0, &answer);
-
 
221
 
-
 
222
    retval = ipc_data_write_start(driver_phone, (char *) name, strlen(name) + 1);
-
 
223
 
-
 
224
    if (retval != EOK) {
-
 
225
        async_wait_for(req, NULL);
-
 
226
        return retval;
-
 
227
    }
-
 
228
 
-
 
229
    async_wait_for(req, &retval);
-
 
230
 
-
 
231
    if (handle != NULL)
-
 
232
        *handle = -1;
-
 
233
   
-
 
234
    if (EOK == retval) {
-
 
235
        if (NULL != handle)
-
 
236
            *handle = (int) IPC_GET_ARG1(answer);
-
 
237
    }
-
 
238
   
-
 
239
    return retval;
-
 
240
}
-
 
241
 
184
/** Prepare the ramdisk image for operation. */
242
/** Prepare the ramdisk image for operation. */
185
static bool rd_init(void)
243
static bool rd_init(void)
186
{
244
{
187
    int retval, flags;
-
 
188
 
-
 
189
    rd_size = sysinfo_value("rd.size");
245
    rd_size = sysinfo_value("rd.size");
190
    void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
246
    void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
191
   
247
   
192
    if (rd_size == 0)
248
    if (rd_size == 0) {
-
 
249
        printf(NAME ": No RAM disk found\n");
193
        return false;
250
        return false;
-
 
251
    }
194
   
252
   
195
    rd_addr = as_get_mappable_page(rd_size);
253
    rd_addr = as_get_mappable_page(rd_size);
196
   
254
   
197
    flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
255
    int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
198
    retval = physmem_map(rd_ph_addr, rd_addr,
256
    int retval = physmem_map(rd_ph_addr, rd_addr,
199
        ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
257
        ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
200
 
258
   
201
    if (retval < 0)
259
    if (retval < 0) {
-
 
260
        printf(NAME ": Error mapping RAM disk\n");
-
 
261
        return false;
-
 
262
    }
-
 
263
   
-
 
264
    printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size);
-
 
265
   
-
 
266
    int driver_phone = driver_register(NAME);
-
 
267
    if (driver_phone < 0) {
-
 
268
        printf(NAME ": Unable to register driver\n");
202
        return false;
269
        return false;
-
 
270
    }
-
 
271
   
-
 
272
    int dev_handle;
-
 
273
    if (EOK != device_register(driver_phone, "initrd", &dev_handle)) {
-
 
274
        ipc_hangup(driver_phone);
-
 
275
        printf(NAME ": Unable to register device\n");
-
 
276
        return false;
-
 
277
    }
-
 
278
   
203
    return true;
279
    return true;
204
}
280
}
205
 
281
 
206
int main(int argc, char **argv)
282
int main(int argc, char **argv)
207
{
283
{
208
    if (rd_init()) {
-
 
209
        ipcarg_t phonead;
-
 
210
       
-
 
211
        async_set_client_connection(rd_connection);
-
 
212
       
-
 
213
        /* Register service at nameserver */
284
    printf(NAME ": HelenOS RAM disk server\n");
214
        if (ipc_connect_to_me(PHONE_NS, SERVICE_RD, 0, 0, &phonead) != 0)
-
 
215
            return -1;
-
 
216
       
285
   
217
        async_manager();
286
    if (!rd_init())
218
       
-
 
219
        /* Never reached */
-
 
220
        return 0;
287
        return -1;
221
    }
-
 
222
   
288
   
-
 
289
    printf(NAME ": Accepting connections\n");
-
 
290
    async_manager();
-
 
291
 
-
 
292
    /* Never reached */
223
    return -1;
293
    return 0;
224
}
294
}
225
 
295
 
226
/**
296
/**
227
 * @}
297
 * @}
228
 */
298
 */