source: mainline/uspace/srv/bd/rd/rd.c@ 89c57b6

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

improve run-time termination

  • get rid of exit()
  • get rid of _exit(), use the common exit()
  • get rid of core(), use the common abort()
  • make main() more fail-safe (call abort() on unhealthy conditions), call async_sess_init() explicitly
  • add several libc-private headers for cleaner environment
  • use SYS_TASK_EXIT in exit() and abort()
  • Property mode set to 100644
File size: 6.8 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 * @{
[d9fae235]34 */
[228b135]35
36/**
[d9fae235]37 * @file rd.c
38 * @brief Initial RAM disk for HelenOS.
[228b135]39 */
40
41#include <ipc/services.h>
42#include <ipc/ns.h>
[7c34822e]43#include <sysinfo.h>
44#include <as.h>
45#include <ddi.h>
46#include <align.h>
47#include <bool.h>
[228b135]48#include <errno.h>
49#include <async.h>
[ff3a34b]50#include <align.h>
51#include <async.h>
[1e4cada]52#include <fibril_synch.h>
[2f65fb0]53#include <stdio.h>
[1090b8c]54#include <devmap.h>
[c5747fe]55#include <ipc/bd.h>
[1ee00b7]56#include <macros.h>
[228b135]57
[2f65fb0]58#define NAME "rd"
59
[1ee00b7]60/** Pointer to the ramdisk's image */
[ff3a34b]61static void *rd_addr;
[d9fae235]62
[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
[d9fae235]72/** This rwlock protects the ramdisk's data.
73 *
[84947a4]74 * If we were to serve multiple requests (read + write or several writes)
[d9fae235]75 * concurrently (i.e. from two or more threads), each read and write needs to
76 * be protected by this rwlock.
77 *
78 */
[52e4f526]79fibril_rwlock_t rd_lock;
[84947a4]80
81/** Handle one connection to ramdisk.
82 *
[d9fae235]83 * @param iid Hash of the request that opened the connection.
84 * @param icall Call data of the request that opened the connection.
[84947a4]85 */
[228b135]86static void rd_connection(ipc_callid_t iid, ipc_call_t *icall)
87{
88 ipc_callid_t callid;
89 ipc_call_t call;
90 int retval;
[3ae470a]91 void *fs_va = NULL;
[1ee00b7]92 uint64_t ba;
93 size_t cnt;
94 size_t comm_size;
[d9fae235]95
[84947a4]96 /*
[c544c5d]97 * Answer the first IPC_M_CONNECT_ME_TO call.
[84947a4]98 */
[ffa2c8ef]99 async_answer_0(iid, EOK);
[d9fae235]100
[84947a4]101 /*
102 * Now we wait for the client to send us its communication as_area.
103 */
[47b7006]104 unsigned int flags;
[0da4e41]105 if (async_share_out_receive(&callid, &comm_size, &flags)) {
[1ee00b7]106 fs_va = as_get_mappable_page(comm_size);
[c544c5d]107 if (fs_va) {
[0da4e41]108 (void) async_share_out_finalize(callid, fs_va);
[84947a4]109 } else {
[ffa2c8ef]110 async_answer_0(callid, EHANGUP);
[d9fae235]111 return;
[84947a4]112 }
113 } else {
114 /*
115 * The client doesn't speak the same protocol.
116 * At this point we can't handle protocol variations.
117 * Close the connection.
118 */
[ffa2c8ef]119 async_answer_0(callid, EHANGUP);
[84947a4]120 return;
121 }
122
[cb41a5e]123 while (true) {
[228b135]124 callid = async_get_call(&call);
[228e490]125 switch (IPC_GET_IMETHOD(call)) {
[8b243f2]126 case IPC_M_PHONE_HUNGUP:
[84947a4]127 /*
128 * The other side has hung up.
[0f78e74]129 * Answer the message and exit the fibril.
[84947a4]130 */
[ffa2c8ef]131 async_answer_0(callid, EOK);
[8b243f2]132 return;
[1ee00b7]133 case BD_READ_BLOCKS:
134 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
135 IPC_GET_ARG2(call));
136 cnt = IPC_GET_ARG3(call);
137 if (cnt * block_size > comm_size) {
[3ae470a]138 retval = ELIMIT;
139 break;
140 }
[1ee00b7]141 retval = rd_read_blocks(ba, cnt, fs_va);
[84947a4]142 break;
[1ee00b7]143 case BD_WRITE_BLOCKS:
144 ba = MERGE_LOUP32(IPC_GET_ARG1(call),
145 IPC_GET_ARG2(call));
146 cnt = IPC_GET_ARG3(call);
147 if (cnt * block_size > comm_size) {
[c544c5d]148 retval = ELIMIT;
149 break;
150 }
[1ee00b7]151 retval = rd_write_blocks(ba, cnt, fs_va);
[8b243f2]152 break;
[1ee00b7]153 case BD_GET_BLOCK_SIZE:
[ffa2c8ef]154 async_answer_1(callid, EOK, block_size);
[1ee00b7]155 continue;
[08232ee]156 case BD_GET_NUM_BLOCKS:
[ffa2c8ef]157 async_answer_2(callid, EOK, LOWER32(rd_size / block_size),
[08232ee]158 UPPER32(rd_size / block_size));
159 continue;
[8b243f2]160 default:
[84947a4]161 /*
162 * The client doesn't speak the same protocol.
163 * Instead of closing the connection, we just ignore
164 * the call. This can be useful if the client uses a
165 * newer version of the protocol.
166 */
[8b243f2]167 retval = EINVAL;
[84947a4]168 break;
[228b135]169 }
[ffa2c8ef]170 async_answer_0(callid, retval);
[8b243f2]171 }
[228b135]172}
173
[1ee00b7]174/** Read blocks from the device. */
175static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf)
176{
177 if ((ba + cnt) * block_size > rd_size) {
178 /* Reading past the end of the device. */
179 return ELIMIT;
180 }
[d9fae235]181
[1ee00b7]182 fibril_rwlock_read_lock(&rd_lock);
183 memcpy(buf, rd_addr + ba * block_size, block_size * cnt);
184 fibril_rwlock_read_unlock(&rd_lock);
[d9fae235]185
[1ee00b7]186 return EOK;
187}
188
189/** Write blocks to the device. */
190static int rd_write_blocks(uint64_t ba, size_t cnt, const void *buf)
191{
192 if ((ba + cnt) * block_size > rd_size) {
193 /* Writing past the end of the device. */
194 return ELIMIT;
195 }
[d9fae235]196
[1ee00b7]197 fibril_rwlock_write_lock(&rd_lock);
198 memcpy(rd_addr + ba * block_size, buf, block_size * cnt);
199 fibril_rwlock_write_unlock(&rd_lock);
[d9fae235]200
[1ee00b7]201 return EOK;
202}
203
[84947a4]204/** Prepare the ramdisk image for operation. */
[7c34822e]205static bool rd_init(void)
[228b135]206{
[d9fae235]207 int ret = sysinfo_get_value("rd.size", &rd_size);
208 if ((ret != EOK) || (rd_size == 0)) {
209 printf("%s: No RAM disk found\n", NAME);
210 return false;
211 }
[7c34822e]212
[d9fae235]213 sysarg_t rd_ph_addr;
214 ret = sysinfo_get_value("rd.address.physical", &rd_ph_addr);
215 if ((ret != EOK) || (rd_ph_addr == 0)) {
216 printf("%s: Invalid RAM disk physical address\n", NAME);
[7c34822e]217 return false;
[2f65fb0]218 }
[228b135]219
[ff3a34b]220 rd_addr = as_get_mappable_page(rd_size);
[228b135]221
[2f65fb0]222 int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
[d9fae235]223 int retval = physmem_map((void *) rd_ph_addr, rd_addr,
[8b243f2]224 ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
[ae318d3]225
[2f65fb0]226 if (retval < 0) {
[d9fae235]227 printf("%s: Error mapping RAM disk\n", NAME);
[2f65fb0]228 return false;
229 }
230
[7e752b2]231 printf("%s: Found RAM disk at %p, %zu bytes\n", NAME,
232 (void *) rd_ph_addr, rd_size);
[2f65fb0]233
[cb41a5e]234 int rc = devmap_driver_register(NAME, rd_connection);
235 if (rc < 0) {
[d9fae235]236 printf("%s: Unable to register driver (%d)\n", NAME, rc);
[2f65fb0]237 return false;
238 }
239
[991f645]240 devmap_handle_t devmap_handle;
241 if (devmap_device_register("bd/initrd", &devmap_handle) != EOK) {
[d9fae235]242 printf("%s: Unable to register device\n", NAME);
[537611cc]243 return false;
244 }
[52e4f526]245
246 fibril_rwlock_initialize(&rd_lock);
[2f65fb0]247
[7c34822e]248 return true;
249}
[228b135]250
[7c34822e]251int main(int argc, char **argv)
252{
[d9fae235]253 printf("%s: HelenOS RAM disk server\n", NAME);
[7c34822e]254
[2f65fb0]255 if (!rd_init())
256 return -1;
257
[d9fae235]258 printf("%s: Accepting connections\n", NAME);
[2f65fb0]259 async_manager();
260
261 /* Never reached */
262 return 0;
[228b135]263}
264
265/**
266 * @}
[cb41a5e]267 */
Note: See TracBrowser for help on using the repository browser.