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

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

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

  • SYS_AS_GET_UNMAPPED_AREA was removed
  • dummy implementations of SYS_PHYSMEM_UNMAP and SYS_IOSPACE_DISABLE were added for the sake of symmetry (they do nothing yet)
  • SYS_PHYSMEM_MAP and SYS_DMAMEM_MAP were altered to accept (void *) -1 as address space area base and a lower bound
  • kernel as_area_create() and as_area_share() were altered to accept (void *) -1 as address space area base and a lower bound
  • uspace libraries and programs were altered to reflect the new API
  • Property mode set to 100644
File size: 6.8 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 (void) async_share_out_finalize(callid, &fs_va);
108 if (fs_va == (void *) -1) {
109 async_answer_0(callid, EHANGUP);
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 */
118 async_answer_0(callid, EHANGUP);
119 return;
120 }
121
122 while (true) {
123 callid = async_get_call(&call);
124
125 if (!IPC_GET_IMETHOD(call)) {
126 /*
127 * The other side has hung up.
128 * Exit the fibril.
129 */
130 async_answer_0(callid, EOK);
131 return;
132 }
133
134 switch (IPC_GET_IMETHOD(call)) {
135 case BD_READ_BLOCKS:
136 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
137 IPC_GET_ARG2(call));
138 cnt = IPC_GET_ARG3(call);
139 if (cnt * block_size > comm_size) {
140 retval = ELIMIT;
141 break;
142 }
143 retval = rd_read_blocks(ba, cnt, fs_va);
144 break;
145 case BD_WRITE_BLOCKS:
146 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
147 IPC_GET_ARG2(call));
148 cnt = IPC_GET_ARG3(call);
149 if (cnt * block_size > comm_size) {
150 retval = ELIMIT;
151 break;
152 }
153 retval = rd_write_blocks(ba, cnt, fs_va);
154 break;
155 case BD_GET_BLOCK_SIZE:
156 async_answer_1(callid, EOK, block_size);
157 continue;
158 case BD_GET_NUM_BLOCKS:
159 async_answer_2(callid, EOK, LOWER32(rd_size / block_size),
160 UPPER32(rd_size / block_size));
161 continue;
162 default:
163 /*
164 * The client doesn't speak the same protocol.
165 * Instead of closing the connection, we just ignore
166 * the call. This can be useful if the client uses a
167 * newer version of the protocol.
168 */
169 retval = EINVAL;
170 break;
171 }
172 async_answer_0(callid, retval);
173 }
174}
175
176/** Read blocks from the device. */
177static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf)
178{
179 if ((ba + cnt) * block_size > rd_size) {
180 /* Reading past the end of the device. */
181 return ELIMIT;
182 }
183
184 fibril_rwlock_read_lock(&rd_lock);
185 memcpy(buf, rd_addr + ba * block_size, block_size * cnt);
186 fibril_rwlock_read_unlock(&rd_lock);
187
188 return EOK;
189}
190
191/** Write blocks to the device. */
192static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
193{
194 if ((ba + cnt) * block_size > rd_size) {
195 /* Writing past the end of the device. */
196 return ELIMIT;
197 }
198
199 fibril_rwlock_write_lock(&rd_lock);
200 memcpy(rd_addr + ba * block_size, buf, block_size * cnt);
201 fibril_rwlock_write_unlock(&rd_lock);
202
203 return EOK;
204}
205
206/** Prepare the ramdisk image for operation. */
207static bool rd_init(void)
208{
209 sysarg_t size;
210 int ret = sysinfo_get_value("rd.size", &size);
211 if ((ret != EOK) || (size == 0)) {
212 printf("%s: No RAM disk found\n", NAME);
213 return false;
214 }
215
216 sysarg_t addr_phys;
217 ret = sysinfo_get_value("rd.address.physical", &addr_phys);
218 if ((ret != EOK) || (addr_phys == 0)) {
219 printf("%s: Invalid RAM disk physical address\n", NAME);
220 return false;
221 }
222
223 rd_size = ALIGN_UP(size, block_size);
224 unsigned int flags =
225 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
226
227 ret = physmem_map((void *) addr_phys,
228 ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags, &rd_addr);
229 if (ret != EOK) {
230 printf("%s: Error mapping RAM disk\n", NAME);
231 return false;
232 }
233
234 printf("%s: Found RAM disk at %p, %" PRIun " bytes\n", NAME,
235 (void *) addr_phys, size);
236
237 ret = loc_server_register(NAME, rd_connection);
238 if (ret < 0) {
239 printf("%s: Unable to register driver (%d)\n", NAME, ret);
240 return false;
241 }
242
243 service_id_t service_id;
244 ret = loc_service_register("bd/initrd", &service_id);
245 if (ret != EOK) {
246 printf("%s: Unable to register device service\n", NAME);
247 return false;
248 }
249
250 fibril_rwlock_initialize(&rd_lock);
251
252 return true;
253}
254
255int main(int argc, char **argv)
256{
257 printf("%s: HelenOS RAM disk server\n", NAME);
258
259 if (!rd_init())
260 return -1;
261
262 printf("%s: Accepting connections\n", NAME);
263 async_manager();
264
265 /* Never reached */
266 return 0;
267}
268
269/**
270 * @}
271 */
Note: See TracBrowser for help on using the repository browser.