source: mainline/uspace/srv/bd/rd/rd.c@ 1ee00b7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1ee00b7 was 1ee00b7, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Revamp block device interface: (1) block size is fixed, determined by BDD. (2) block address is 64-bit, (3) allow reading/writing more blocks at a time.

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[228b135]1/*
[ff3a34b]2 * Copyright (c) 2007 Michal Konopa
3 * Copyright (c) 2007 Martin Jelen
4 * Copyright (c) 2007 Peter Majer
[84947a4]5 * Copyright (c) 2007 Jakub Jermar
[228b135]6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * - The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/** @addtogroup rd
33 * @{
34 */
35
36/**
37 * @file rd.c
38 * @brief Initial RAM disk for HelenOS.
39 */
40
41#include <ipc/ipc.h>
42#include <ipc/services.h>
43#include <ipc/ns.h>
[7c34822e]44#include <sysinfo.h>
45#include <as.h>
46#include <ddi.h>
47#include <align.h>
48#include <bool.h>
[228b135]49#include <errno.h>
50#include <async.h>
[ff3a34b]51#include <align.h>
52#include <async.h>
[52e4f526]53#include <fibril_sync.h>
[2f65fb0]54#include <stdio.h>
[1090b8c]55#include <devmap.h>
[c5747fe]56#include <ipc/bd.h>
[1ee00b7]57#include <macros.h>
[228b135]58
[2f65fb0]59#define NAME "rd"
60
[1ee00b7]61/** Pointer to the ramdisk's image */
[ff3a34b]62static void *rd_addr;
[1ee00b7]63/** Size of the ramdisk */
[3ae470a]64static size_t rd_size;
[228b135]65
[1ee00b7]66/** Block size */
67static const size_t block_size = 512;
68
69static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf);
70static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
71
[84947a4]72/**
[52e4f526]73 * This rwlock protects the ramdisk's data.
[84947a4]74 * If we were to serve multiple requests (read + write or several writes)
75 * concurrently (i.e. from two or more threads), each read and write needs to be
[52e4f526]76 * protected by this rwlock.
[84947a4]77 */
[52e4f526]78fibril_rwlock_t rd_lock;
[84947a4]79
80/** Handle one connection to ramdisk.
81 *
82 * @param iid Hash of the request that opened the connection.
83 * @param icall Call data of the request that opened the connection.
84 */
[228b135]85static void rd_connection(ipc_callid_t iid, ipc_call_t *icall)
86{
87 ipc_callid_t callid;
88 ipc_call_t call;
89 int retval;
[3ae470a]90 void *fs_va = NULL;
[1ee00b7]91 uint64_t ba;
92 size_t cnt;
93 size_t comm_size;
[228b135]94
[84947a4]95 /*
[c544c5d]96 * Answer the first IPC_M_CONNECT_ME_TO call.
[84947a4]97 */
[c544c5d]98 ipc_answer_0(iid, EOK);
[84947a4]99
100 /*
101 * Now we wait for the client to send us its communication as_area.
102 */
[2f65fb0]103 int flags;
[1ee00b7]104 if (ipc_share_out_receive(&callid, &comm_size, &flags)) {
105 fs_va = as_get_mappable_page(comm_size);
[c544c5d]106 if (fs_va) {
[215e375]107 (void) ipc_share_out_finalize(callid, fs_va);
[84947a4]108 } else {
[b74959bd]109 ipc_answer_0(callid, EHANGUP);
[84947a4]110 return;
111 }
112 } else {
113 /*
114 * The client doesn't speak the same protocol.
115 * At this point we can't handle protocol variations.
116 * Close the connection.
117 */
[b74959bd]118 ipc_answer_0(callid, EHANGUP);
[84947a4]119 return;
120 }
121
[cb41a5e]122 while (true) {
[228b135]123 callid = async_get_call(&call);
124 switch (IPC_GET_METHOD(call)) {
[8b243f2]125 case IPC_M_PHONE_HUNGUP:
[84947a4]126 /*
127 * The other side has hung up.
[0f78e74]128 * Answer the message and exit the fibril.
[84947a4]129 */
[b74959bd]130 ipc_answer_0(callid, EOK);
[8b243f2]131 return;
[1ee00b7]132 case BD_READ_BLOCKS:
133 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
134 IPC_GET_ARG2(call));
135 cnt = IPC_GET_ARG3(call);
136 if (cnt * block_size > comm_size) {
[3ae470a]137 retval = ELIMIT;
138 break;
139 }
[1ee00b7]140 retval = rd_read_blocks(ba, cnt, fs_va);
[84947a4]141 break;
[1ee00b7]142 case BD_WRITE_BLOCKS:
143 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
144 IPC_GET_ARG2(call));
145 cnt = IPC_GET_ARG3(call);
146 if (cnt * block_size > comm_size) {
[c544c5d]147 retval = ELIMIT;
148 break;
149 }
[1ee00b7]150 retval = rd_write_blocks(ba, cnt, fs_va);
[8b243f2]151 break;
[1ee00b7]152 case BD_GET_BLOCK_SIZE:
153 ipc_answer_1(callid, EOK, block_size);
154 continue;
[8b243f2]155 default:
[84947a4]156 /*
157 * The client doesn't speak the same protocol.
158 * Instead of closing the connection, we just ignore
159 * the call. This can be useful if the client uses a
160 * newer version of the protocol.
161 */
[8b243f2]162 retval = EINVAL;
[84947a4]163 break;
[228b135]164 }
[b74959bd]165 ipc_answer_0(callid, retval);
[8b243f2]166 }
[228b135]167}
168
[1ee00b7]169/** Read blocks from the device. */
170static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf)
171{
172 if ((ba + cnt) * block_size > rd_size) {
173 /* Reading past the end of the device. */
174 return ELIMIT;
175 }
176
177 fibril_rwlock_read_lock(&rd_lock);
178 memcpy(buf, rd_addr + ba * block_size, block_size * cnt);
179 fibril_rwlock_read_unlock(&rd_lock);
180
181 return EOK;
182}
183
184/** Write blocks to the device. */
185static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
186{
187 if ((ba + cnt) * block_size > rd_size) {
188 /* Writing past the end of the device. */
189 return ELIMIT;
190 }
191
192 fibril_rwlock_write_lock(&rd_lock);
193 memcpy(rd_addr + ba * block_size, buf, block_size * cnt);
194 fibril_rwlock_write_unlock(&rd_lock);
195
196 return EOK;
197}
198
[84947a4]199/** Prepare the ramdisk image for operation. */
[7c34822e]200static bool rd_init(void)
[228b135]201{
[3ae470a]202 rd_size = sysinfo_value("rd.size");
[8b243f2]203 void *rd_ph_addr = (void *) sysinfo_value("rd.address.physical");
[7c34822e]204
[2f65fb0]205 if (rd_size == 0) {
206 printf(NAME ": No RAM disk found\n");
[7c34822e]207 return false;
[2f65fb0]208 }
[228b135]209
[ff3a34b]210 rd_addr = as_get_mappable_page(rd_size);
[228b135]211
[2f65fb0]212 int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
213 int retval = physmem_map(rd_ph_addr, rd_addr,
[8b243f2]214 ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
[ae318d3]215
[2f65fb0]216 if (retval < 0) {
217 printf(NAME ": Error mapping RAM disk\n");
218 return false;
219 }
220
221 printf(NAME ": Found RAM disk at %p, %d bytes\n", rd_ph_addr, rd_size);
222
[cb41a5e]223 int rc = devmap_driver_register(NAME, rd_connection);
224 if (rc < 0) {
225 printf(NAME ": Unable to register driver (%d)\n", rc);
[2f65fb0]226 return false;
227 }
228
[854c564]229 dev_handle_t dev_handle;
[cb41a5e]230 if (devmap_device_register("initrd", &dev_handle) != EOK) {
231 devmap_hangup_phone(DEVMAP_DRIVER);
[537611cc]232 printf(NAME ": Unable to register device\n");
233 return false;
234 }
[52e4f526]235
236 fibril_rwlock_initialize(&rd_lock);
[2f65fb0]237
[7c34822e]238 return true;
239}
[228b135]240
[7c34822e]241int main(int argc, char **argv)
242{
[2f65fb0]243 printf(NAME ": HelenOS RAM disk server\n");
[7c34822e]244
[2f65fb0]245 if (!rd_init())
246 return -1;
247
248 printf(NAME ": Accepting connections\n");
249 async_manager();
250
251 /* Never reached */
252 return 0;
[228b135]253}
254
255/**
256 * @}
[cb41a5e]257 */
Note: See TracBrowser for help on using the repository browser.