source: mainline/uspace/srv/bd/rd/rd.c@ b5daa89

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b5daa89 was b5daa89, checked in by Martin Decky <martin@…>, 14 years ago

align RAM disk size to block size
cstyle

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * Copyright (c) 2007 Michal Konopa
3 * Copyright (c) 2007 Martin Jelen
4 * Copyright (c) 2007 Peter Majer
5 * Copyright (c) 2007 Jakub Jermar
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/services.h>
42#include <ipc/ns.h>
43#include <sysinfo.h>
44#include <as.h>
45#include <ddi.h>
46#include <align.h>
47#include <bool.h>
48#include <errno.h>
49#include <async.h>
50#include <align.h>
51#include <async.h>
52#include <fibril_synch.h>
53#include <stdio.h>
54#include <loc.h>
55#include <ipc/bd.h>
56#include <macros.h>
57#include <inttypes.h>
58
59#define NAME "rd"
60
61/** Pointer to the ramdisk's image */
62static void *rd_addr;
63
64/** Size of the ramdisk */
65static size_t rd_size;
66
67/** Block size */
68static const size_t block_size = 512;
69
70static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf);
71static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf);
72
73/** This rwlock protects the ramdisk's data.
74 *
75 * If we were to serve multiple requests (read + write or several writes)
76 * concurrently (i.e. from two or more threads), each read and write needs to
77 * be protected by this rwlock.
78 *
79 */
80fibril_rwlock_t rd_lock;
81
82/** Handle one connection to ramdisk.
83 *
84 * @param iid Hash of the request that opened the connection.
85 * @param icall Call data of the request that opened the connection.
86 */
87static void rd_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
88{
89 ipc_callid_t callid;
90 ipc_call_t call;
91 int retval;
92 void *fs_va = NULL;
93 uint64_t ba;
94 size_t cnt;
95 size_t comm_size;
96
97 /*
98 * Answer the first IPC_M_CONNECT_ME_TO call.
99 */
100 async_answer_0(iid, EOK);
101
102 /*
103 * Now we wait for the client to send us its communication as_area.
104 */
105 unsigned int flags;
106 if (async_share_out_receive(&callid, &comm_size, &flags)) {
107 fs_va = as_get_mappable_page(comm_size);
108 if (fs_va) {
109 (void) async_share_out_finalize(callid, fs_va);
110 } else {
111 async_answer_0(callid, EHANGUP);
112 return;
113 }
114 } else {
115 /*
116 * The client doesn't speak the same protocol.
117 * At this point we can't handle protocol variations.
118 * Close the connection.
119 */
120 async_answer_0(callid, EHANGUP);
121 return;
122 }
123
124 while (true) {
125 callid = async_get_call(&call);
126
127 if (!IPC_GET_IMETHOD(call)) {
128 /*
129 * The other side has hung up.
130 * Exit the fibril.
131 */
132 async_answer_0(callid, EOK);
133 return;
134 }
135
136 switch (IPC_GET_IMETHOD(call)) {
137 case BD_READ_BLOCKS:
138 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
139 IPC_GET_ARG2(call));
140 cnt = IPC_GET_ARG3(call);
141 if (cnt * block_size > comm_size) {
142 retval = ELIMIT;
143 break;
144 }
145 retval = rd_read_blocks(ba, cnt, fs_va);
146 break;
147 case BD_WRITE_BLOCKS:
148 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
149 IPC_GET_ARG2(call));
150 cnt = IPC_GET_ARG3(call);
151 if (cnt * block_size > comm_size) {
152 retval = ELIMIT;
153 break;
154 }
155 retval = rd_write_blocks(ba, cnt, fs_va);
156 break;
157 case BD_GET_BLOCK_SIZE:
158 async_answer_1(callid, EOK, block_size);
159 continue;
160 case BD_GET_NUM_BLOCKS:
161 async_answer_2(callid, EOK, LOWER32(rd_size / block_size),
162 UPPER32(rd_size / block_size));
163 continue;
164 default:
165 /*
166 * The client doesn't speak the same protocol.
167 * Instead of closing the connection, we just ignore
168 * the call. This can be useful if the client uses a
169 * newer version of the protocol.
170 */
171 retval = EINVAL;
172 break;
173 }
174 async_answer_0(callid, retval);
175 }
176}
177
178/** Read blocks from the device. */
179static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf)
180{
181 if ((ba + cnt) * block_size > rd_size) {
182 /* Reading past the end of the device. */
183 return ELIMIT;
184 }
185
186 fibril_rwlock_read_lock(&rd_lock);
187 memcpy(buf, rd_addr + ba * block_size, block_size * cnt);
188 fibril_rwlock_read_unlock(&rd_lock);
189
190 return EOK;
191}
192
193/** Write blocks to the device. */
194static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
195{
196 if ((ba + cnt) * block_size > rd_size) {
197 /* Writing past the end of the device. */
198 return ELIMIT;
199 }
200
201 fibril_rwlock_write_lock(&rd_lock);
202 memcpy(rd_addr + ba * block_size, buf, block_size * cnt);
203 fibril_rwlock_write_unlock(&rd_lock);
204
205 return EOK;
206}
207
208/** Prepare the ramdisk image for operation. */
209static bool rd_init(void)
210{
211 sysarg_t size;
212 int ret = sysinfo_get_value("rd.size", &size);
213 if ((ret != EOK) || (size == 0)) {
214 printf("%s: No RAM disk found\n", NAME);
215 return false;
216 }
217
218 sysarg_t addr_phys;
219 ret = sysinfo_get_value("rd.address.physical", &addr_phys);
220 if ((ret != EOK) || (addr_phys == 0)) {
221 printf("%s: Invalid RAM disk physical address\n", NAME);
222 return false;
223 }
224
225 rd_size = ALIGN_UP(size, block_size);
226 rd_addr = as_get_mappable_page(rd_size);
227
228 unsigned int flags =
229 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
230 ret = physmem_map((void *) addr_phys, rd_addr,
231 ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
232 if (ret < 0) {
233 printf("%s: Error mapping RAM disk\n", NAME);
234 return false;
235 }
236
237 printf("%s: Found RAM disk at %p, %" PRIun " bytes\n", NAME,
238 (void *) addr_phys, size);
239
240 ret = loc_server_register(NAME, rd_connection);
241 if (ret < 0) {
242 printf("%s: Unable to register driver (%d)\n", NAME, ret);
243 return false;
244 }
245
246 service_id_t service_id;
247 ret = loc_service_register("bd/initrd", &service_id);
248 if (ret != EOK) {
249 printf("%s: Unable to register device service\n", NAME);
250 return false;
251 }
252
253 fibril_rwlock_initialize(&rd_lock);
254
255 return true;
256}
257
258int main(int argc, char **argv)
259{
260 printf("%s: HelenOS RAM disk server\n", NAME);
261
262 if (!rd_init())
263 return -1;
264
265 printf("%s: Accepting connections\n", NAME);
266 async_manager();
267
268 /* Never reached */
269 return 0;
270}
271
272/**
273 * @}
274 */
Note: See TracBrowser for help on using the repository browser.