source: mainline/uspace/srv/bd/rd/rd.c@ 357b5f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 357b5f5 was 7ea7db31, checked in by Jakub Jermar <jakub@…>, 15 years ago

Cease using devmap_get_phone() and devmap_hangup_phone() in drivers.

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