| 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/ipc.h>
|
|---|
| 42 | #include <ipc/services.h>
|
|---|
| 43 | #include <ipc/ns.h>
|
|---|
| 44 | #include <sysinfo.h>
|
|---|
| 45 | #include <as.h>
|
|---|
| 46 | #include <ddi.h>
|
|---|
| 47 | #include <align.h>
|
|---|
| 48 | #include <bool.h>
|
|---|
| 49 | #include <errno.h>
|
|---|
| 50 | #include <async.h>
|
|---|
| 51 | #include <align.h>
|
|---|
| 52 | #include <async.h>
|
|---|
| 53 | #include <fibril_synch.h>
|
|---|
| 54 | #include <stdio.h>
|
|---|
| 55 | #include <devmap.h>
|
|---|
| 56 | #include <ipc/bd.h>
|
|---|
| 57 | #include <macros.h>
|
|---|
| 58 |
|
|---|
| 59 | #define NAME "rd"
|
|---|
| 60 |
|
|---|
| 61 | /** Pointer to the ramdisk's image */
|
|---|
| 62 | static void *rd_addr;
|
|---|
| 63 |
|
|---|
| 64 | /** Size of the ramdisk */
|
|---|
| 65 | static size_t rd_size;
|
|---|
| 66 |
|
|---|
| 67 | /** Block size */
|
|---|
| 68 | static const size_t block_size = 512;
|
|---|
| 69 |
|
|---|
| 70 | static int rd_read_blocks(uint64_t ba, size_t cnt, void *buf);
|
|---|
| 71 | static 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 | */
|
|---|
| 80 | fibril_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 | */
|
|---|
| 87 | static 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;
|
|---|
| 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 | ipc_answer_0(iid, EOK);
|
|---|
| 101 |
|
|---|
| 102 | /*
|
|---|
| 103 | * Now we wait for the client to send us its communication as_area.
|
|---|
| 104 | */
|
|---|
| 105 | 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 | ipc_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 | ipc_answer_0(callid, EHANGUP);
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | while (true) {
|
|---|
| 125 | callid = async_get_call(&call);
|
|---|
| 126 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 127 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 128 | /*
|
|---|
| 129 | * The other side has hung up.
|
|---|
| 130 | * Answer the message and exit the fibril.
|
|---|
| 131 | */
|
|---|
| 132 | ipc_answer_0(callid, EOK);
|
|---|
| 133 | return;
|
|---|
| 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) {
|
|---|
| 139 | retval = ELIMIT;
|
|---|
| 140 | break;
|
|---|
| 141 | }
|
|---|
| 142 | retval = rd_read_blocks(ba, cnt, fs_va);
|
|---|
| 143 | break;
|
|---|
| 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) {
|
|---|
| 149 | retval = ELIMIT;
|
|---|
| 150 | break;
|
|---|
| 151 | }
|
|---|
| 152 | retval = rd_write_blocks(ba, cnt, fs_va);
|
|---|
| 153 | break;
|
|---|
| 154 | case BD_GET_BLOCK_SIZE:
|
|---|
| 155 | ipc_answer_1(callid, EOK, block_size);
|
|---|
| 156 | continue;
|
|---|
| 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;
|
|---|
| 161 | default:
|
|---|
| 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 | */
|
|---|
| 168 | retval = EINVAL;
|
|---|
| 169 | break;
|
|---|
| 170 | }
|
|---|
| 171 | ipc_answer_0(callid, retval);
|
|---|
| 172 | }
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | /** Read blocks from the device. */
|
|---|
| 176 | static 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 | }
|
|---|
| 182 |
|
|---|
| 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);
|
|---|
| 186 |
|
|---|
| 187 | return EOK;
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | /** Write blocks to the device. */
|
|---|
| 191 | static 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 | }
|
|---|
| 197 |
|
|---|
| 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);
|
|---|
| 201 |
|
|---|
| 202 | return EOK;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** Prepare the ramdisk image for operation. */
|
|---|
| 206 | static bool rd_init(void)
|
|---|
| 207 | {
|
|---|
| 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 | }
|
|---|
| 213 |
|
|---|
| 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);
|
|---|
| 218 | return false;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | rd_addr = as_get_mappable_page(rd_size);
|
|---|
| 222 |
|
|---|
| 223 | int flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
|
|---|
| 224 | int retval = physmem_map((void *) rd_ph_addr, rd_addr,
|
|---|
| 225 | ALIGN_UP(rd_size, PAGE_SIZE) >> PAGE_WIDTH, flags);
|
|---|
| 226 |
|
|---|
| 227 | if (retval < 0) {
|
|---|
| 228 | printf("%s: Error mapping RAM disk\n", NAME);
|
|---|
| 229 | return false;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | printf("%s: Found RAM disk at %p, %zu bytes\n", NAME,
|
|---|
| 233 | (void *) rd_ph_addr, rd_size);
|
|---|
| 234 |
|
|---|
| 235 | int rc = devmap_driver_register(NAME, rd_connection);
|
|---|
| 236 | if (rc < 0) {
|
|---|
| 237 | printf("%s: Unable to register driver (%d)\n", NAME, rc);
|
|---|
| 238 | return false;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | devmap_handle_t devmap_handle;
|
|---|
| 242 | if (devmap_device_register("bd/initrd", &devmap_handle) != EOK) {
|
|---|
| 243 | printf("%s: Unable to register device\n", NAME);
|
|---|
| 244 | return false;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | fibril_rwlock_initialize(&rd_lock);
|
|---|
| 248 |
|
|---|
| 249 | return true;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | int main(int argc, char **argv)
|
|---|
| 253 | {
|
|---|
| 254 | printf("%s: HelenOS RAM disk server\n", NAME);
|
|---|
| 255 |
|
|---|
| 256 | if (!rd_init())
|
|---|
| 257 | return -1;
|
|---|
| 258 |
|
|---|
| 259 | printf("%s: Accepting connections\n", NAME);
|
|---|
| 260 | async_manager();
|
|---|
| 261 |
|
|---|
| 262 | /* Never reached */
|
|---|
| 263 | return 0;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * @}
|
|---|
| 268 | */
|
|---|