| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Ondrej Palkovsky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup ns
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file ns.c
|
|---|
| 35 | * @brief Naming service for HelenOS IPC.
|
|---|
| 36 | */
|
|---|
| 37 |
|
|---|
| 38 | #include <ipc/ipc.h>
|
|---|
| 39 | #include <ipc/services.h>
|
|---|
| 40 | #include <ipc/ns.h>
|
|---|
| 41 | #include <unistd.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <errno.h>
|
|---|
| 44 | #include <as.h>
|
|---|
| 45 | #include <ddi.h>
|
|---|
| 46 | #include <event.h>
|
|---|
| 47 | #include <macros.h>
|
|---|
| 48 | #include <sysinfo.h>
|
|---|
| 49 | #include "ns.h"
|
|---|
| 50 | #include "service.h"
|
|---|
| 51 | #include "clonable.h"
|
|---|
| 52 | #include "task.h"
|
|---|
| 53 |
|
|---|
| 54 | static void *clockaddr = NULL;
|
|---|
| 55 | static void *klogaddr = NULL;
|
|---|
| 56 |
|
|---|
| 57 | static void get_as_area(ipc_callid_t callid, ipc_call_t *call, void *faddr,
|
|---|
| 58 | size_t pages, void **addr)
|
|---|
| 59 | {
|
|---|
| 60 | if ((faddr == NULL) || (pages == 0)) {
|
|---|
| 61 | ipc_answer_0(callid, ENOENT);
|
|---|
| 62 | return;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | if (*addr == NULL) {
|
|---|
| 66 | *addr = as_get_mappable_page(pages * PAGE_SIZE);
|
|---|
| 67 |
|
|---|
| 68 | if (*addr == NULL) {
|
|---|
| 69 | ipc_answer_0(callid, ENOENT);
|
|---|
| 70 | return;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | if (physmem_map(faddr, *addr, pages,
|
|---|
| 74 | AS_AREA_READ | AS_AREA_CACHEABLE) != 0) {
|
|---|
| 75 | ipc_answer_0(callid, ENOENT);
|
|---|
| 76 | return;
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | ipc_answer_2(callid, EOK, (sysarg_t) *addr, AS_AREA_READ);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | static void setup_clock_area(ipc_callid_t callid, ipc_call_t *call, void **addr)
|
|---|
| 84 | {
|
|---|
| 85 | uintptr_t faddr;
|
|---|
| 86 | int err = sysinfo_get_value("clock.faddr", &faddr);
|
|---|
| 87 |
|
|---|
| 88 | if (err != EOK)
|
|---|
| 89 | ipc_answer_0(callid, err);
|
|---|
| 90 |
|
|---|
| 91 | get_as_area(callid, call, (void *) faddr, 1, addr);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | static void setup_klog_area(ipc_callid_t callid, ipc_call_t *call, void **addr)
|
|---|
| 95 | {
|
|---|
| 96 | uintptr_t faddr;
|
|---|
| 97 | int err = sysinfo_get_value("klog.faddr", &faddr);
|
|---|
| 98 |
|
|---|
| 99 | if (err != EOK)
|
|---|
| 100 | ipc_answer_0(callid, err);
|
|---|
| 101 |
|
|---|
| 102 | size_t pages;
|
|---|
| 103 | err = sysinfo_get_value("klog.pages", &pages);
|
|---|
| 104 |
|
|---|
| 105 | if (err != EOK)
|
|---|
| 106 | ipc_answer_0(callid, err);
|
|---|
| 107 |
|
|---|
| 108 | get_as_area(callid, call, (void *) faddr, pages, addr);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | int main(int argc, char **argv)
|
|---|
| 112 | {
|
|---|
| 113 | printf(NAME ": HelenOS IPC Naming Service\n");
|
|---|
| 114 |
|
|---|
| 115 | int rc = service_init();
|
|---|
| 116 | if (rc != EOK)
|
|---|
| 117 | return rc;
|
|---|
| 118 |
|
|---|
| 119 | rc = clonable_init();
|
|---|
| 120 | if (rc != EOK)
|
|---|
| 121 | return rc;
|
|---|
| 122 |
|
|---|
| 123 | rc = task_init();
|
|---|
| 124 | if (rc != EOK)
|
|---|
| 125 | return rc;
|
|---|
| 126 |
|
|---|
| 127 | printf("%s: Accepting connections\n", NAME);
|
|---|
| 128 |
|
|---|
| 129 | while (true) {
|
|---|
| 130 | process_pending_conn();
|
|---|
| 131 | process_pending_wait();
|
|---|
| 132 |
|
|---|
| 133 | ipc_call_t call;
|
|---|
| 134 | ipc_callid_t callid = ipc_wait_for_call(&call);
|
|---|
| 135 |
|
|---|
| 136 | task_id_t id;
|
|---|
| 137 | sysarg_t retval;
|
|---|
| 138 |
|
|---|
| 139 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 140 | case IPC_M_SHARE_IN:
|
|---|
| 141 | switch (IPC_GET_ARG3(call)) {
|
|---|
| 142 | case SERVICE_MEM_REALTIME:
|
|---|
| 143 | setup_clock_area(callid, &call, &clockaddr);
|
|---|
| 144 | break;
|
|---|
| 145 | case SERVICE_MEM_KLOG:
|
|---|
| 146 | setup_klog_area(callid, &call, &klogaddr);
|
|---|
| 147 | break;
|
|---|
| 148 | default:
|
|---|
| 149 | ipc_answer_0(callid, ENOENT);
|
|---|
| 150 | }
|
|---|
| 151 | continue;
|
|---|
| 152 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 153 | retval = ns_task_disconnect(&call);
|
|---|
| 154 | break;
|
|---|
| 155 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 156 | /*
|
|---|
| 157 | * Server requests service registration.
|
|---|
| 158 | */
|
|---|
| 159 | if (service_clonable(IPC_GET_ARG1(call))) {
|
|---|
| 160 | register_clonable(IPC_GET_ARG1(call),
|
|---|
| 161 | IPC_GET_ARG5(call), &call, callid);
|
|---|
| 162 | continue;
|
|---|
| 163 | } else {
|
|---|
| 164 | retval = register_service(IPC_GET_ARG1(call),
|
|---|
| 165 | IPC_GET_ARG5(call), &call);
|
|---|
| 166 | }
|
|---|
| 167 | break;
|
|---|
| 168 | case IPC_M_CONNECT_ME_TO:
|
|---|
| 169 | /*
|
|---|
| 170 | * Client requests to be connected to a service.
|
|---|
| 171 | */
|
|---|
| 172 | if (service_clonable(IPC_GET_ARG1(call))) {
|
|---|
| 173 | connect_to_clonable(IPC_GET_ARG1(call),
|
|---|
| 174 | &call, callid);
|
|---|
| 175 | continue;
|
|---|
| 176 | } else {
|
|---|
| 177 | connect_to_service(IPC_GET_ARG1(call), &call,
|
|---|
| 178 | callid);
|
|---|
| 179 | continue;
|
|---|
| 180 | }
|
|---|
| 181 | break;
|
|---|
| 182 | case NS_PING:
|
|---|
| 183 | retval = EOK;
|
|---|
| 184 | break;
|
|---|
| 185 | case NS_TASK_WAIT:
|
|---|
| 186 | id = (task_id_t)
|
|---|
| 187 | MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
|---|
| 188 | wait_for_task(id, &call, callid);
|
|---|
| 189 | continue;
|
|---|
| 190 | case NS_ID_INTRO:
|
|---|
| 191 | retval = ns_task_id_intro(&call);
|
|---|
| 192 | break;
|
|---|
| 193 | case NS_RETVAL:
|
|---|
| 194 | retval = ns_task_retval(&call);
|
|---|
| 195 | break;
|
|---|
| 196 | default:
|
|---|
| 197 | retval = ENOENT;
|
|---|
| 198 | break;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
|---|
| 202 | ipc_answer_0(callid, retval);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /* Not reached */
|
|---|
| 206 | return 0;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | /**
|
|---|
| 210 | * @}
|
|---|
| 211 | */
|
|---|