1/*
2 * Copyright 2016, Data61
3 * Commonwealth Scientific and Industrial Research Organisation (CSIRO)
4 * ABN 41 687 119 230.
5 *
6 * This software may be distributed and modified according to the terms of
7 * the BSD 2-Clause license. Note that NO WARRANTY is provided.
8 * See "LICENSE_BSD2.txt" for details.
9 *
10 * @TAG(D61_BSD)
11 */
12
13#include <assert.h>
14#include <stdbool.h>
15#include <stdlib.h>
16#include <sel4/sel4.h>
17
18#include <refos-rpc/rpc.h>
19#include <refos/refos.h>
20#include <refos/vmlayout.h>
21#include <refos-util/dprintf.h>
22#include <refos-util/cspace.h>
23
24seL4_CPtr rpc_copyout_cptr(seL4_CPtr v) __attribute__((weak));
25seL4_CPtr
26rpc_copyout_cptr(seL4_CPtr v)
27{
28    /* Note that this is used to implement malloc itself , so we can NOT malloc here anywhere
29       in this function. */
30
31    if (!v) {
32        /* Don't copyout a NULL cap. */
33        return (seL4_CPtr) 0;
34    }
35
36    /* Allocate a new cslot and copy the cap out from the shared recieve slot.
37       Remember that this function passes ownership of the cap, so the caller
38       needs to delete the cap and free the slot, akin to void *mem = malloc(x);
39    */
40    seL4_CPtr cslot = csalloc();
41    if (cslot == 0) {
42        REFOS_SET_ERRNO(ENOMEM);
43        return (seL4_CPtr) 0;
44    }
45    int error = seL4_CNode_Move(
46        REFOS_CSPACE, cslot, REFOS_CSPACE_DEPTH,
47        REFOS_CSPACE, v, REFOS_CSPACE_DEPTH
48    );
49    assert(error == seL4_NoError);
50    (void) error;
51
52    return cslot;
53}
54
55ENDPT rpc_sv_get_reply_endpoint(void *cl) __attribute__((weak));
56ENDPT
57rpc_sv_get_reply_endpoint(void *cl)
58{
59    rpc_client_state_t *c = (rpc_client_state_t *)cl;
60    if (!c) {
61        return 0;
62    }
63    return c->reply;
64}
65
66bool rpc_sv_skip_reply(void *cl) __attribute__((weak));
67bool
68rpc_sv_skip_reply(void *cl)
69{
70    rpc_client_state_t *c = (rpc_client_state_t *)cl;
71    if (!c) return false;
72    return c->skip_reply;
73}
74
75void
76rpc_helper_client_release(void *cl)
77{
78    rpc_client_state_t *c = (rpc_client_state_t*) cl;
79    if (!c) return;
80    assert(c->num_obj == 0);
81
82    // Delete the client's reply cap slot.
83    if (c->reply) {
84        seL4_CNode_Revoke(REFOS_CSPACE, c->reply, REFOS_CSPACE_DEPTH);
85        csfree_delete(c->reply);
86        c->reply = 0;
87    }
88}
89
90
91
92