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