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#ifndef _REFOS_PROCESS_SERVER_STATE_H_
14#define _REFOS_PROCESS_SERVER_STATE_H_
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <assert.h>
19#include <sel4/sel4.h>
20#include <autoconf.h>
21#include <allocman/vka.h>
22#include <allocman/allocman.h>
23#include <allocman/bootstrap.h>
24#include <allocman/cspaceops.h>
25#include <vka/vka.h>
26#include <vka/object.h>
27#include <vka/capops.h>
28#include <vspace/vspace.h>
29#include <sel4utils/vspace.h>
30#include <refos-util/nameserv.h>
31#include <sel4platsupport/platsupport.h>
32#include <data_struct/chash.h>
33#include <simple/simple.h>
34#include <simple-default/simple-default.h>
35
36#include "common.h"
37#include "system/process/pid.h"
38#include "system/addrspace/vspace.h"
39#include "system/addrspace/pagedir.h"
40#include "system/memserv/window.h"
41#include "system/memserv/dataspace.h"
42
43/*! @file
44    @brief Global environment struct & helper functions for process server. */
45
46/*! @brief A list of global process server objects; represents an instance of the process server. */
47struct procserv_state {
48    /* Allocator information. */
49    seL4_BootInfo                     *bootinfo;
50    vka_t                              vka;
51    vka_t                             *vkaPtr;
52    vspace_t                           vspace;
53    sel4utils_alloc_data_t             vspaceData;
54    allocman_t                        *allocman;
55    simple_t                           simpleEnv;
56#ifdef CONFIG_ARCH_ARM
57    /* Copy of simpleEnv.frame_cap function (required for function wrapper). */
58    simple_get_frame_cap_fn            original_simple_get_frame_cap;
59
60    /* Cspace path to the serial frame cap. */
61    cspacepath_t                       serial_frame_cap_path;
62#endif /* CONFIG_ARCH_ARM */
63    /* Process server endpoints. */
64    vka_object_t                       endpoint;
65    cspacepath_t                       IPCCapRecv;
66
67    /* Process server global lists. */
68    struct pid_list                    PIDList;
69    struct pd_list                     PDList;
70    struct w_list                      windowList;
71    struct ram_dspace_list             dspaceList;
72    nameserv_state_t                   nameServRegList;
73    chash_t                            irqHandlerList;
74
75    /* Misc states. */
76    uint32_t                           faketime;
77    uint32_t                           unblockClientFaultPID;
78    uint32_t                           exitProcessPID;
79};
80
81/*! @brief Process server message structure. */
82struct procserv_msg {
83    seL4_MessageInfo_t message;
84    seL4_Word badge;
85    struct procserv_state *state;
86};
87
88/*! @brief Process server CPIO archive. */
89extern char _refos_process_server_archive[];
90
91/*! @brief Process server global state. */
92extern struct procserv_state procServ;
93
94/*! @brief Fake time, useful for debugging. */
95uint32_t faketime();
96
97/*! @brief Initialises the process server.
98
99    High-level initialisation function which calls the other initialisation functions and starts the
100    process server up and running to the point where it is ready to dispatch all messages, and is
101    able to start processes.
102
103    @param info The BootInfo struct passed in from the kernel.
104    @param s The process server global state.
105 */
106void initialise(seL4_BootInfo *info, struct procserv_state *s);
107
108/*! @brief Creates a generic minted badge using the process server's EP.
109
110    Creates a generic minted badge using the process server's EP. Ownership is transferred, so the
111    resulting capability must be revoked, deleted and the cslot freed by the caller. Permissions are
112    set to canWrite | canGrant, as the resulting object badge should be passable and invokable but
113    we don't really want anyone else listening in to that EP and pretending to be the process
114    server.
115
116    @param badge the badge number to create.
117    @return cslot to minted badge (ownership transfer).
118*/
119cspacepath_t procserv_mint_badge(int badge);
120
121/*! @brief Temporary map a page frame and write data to it.
122
123    In order to read / write data to / from a
124
125    @param frame CPtr to destination frame.
126    @param src Data source buffer.
127    @param len Data source buffer length.
128    @param offset Offset into frame to write to.
129    @return ESUCCESS if write successful, refos error otherwise.
130*/
131int procserv_frame_write(seL4_CPtr frame, const char* src, size_t len, size_t offset);
132
133/*! @brief Temporary map a page frame and read data from it.
134    @param frame CPtr to source frame.
135    @param dst Data destination buffer.
136    @param len Data destination buffer max length.
137    @param offset Offset into frame to read from.
138    @return ESUCCESS if read successful, refos error otherwise.
139*/
140int procserv_frame_read(seL4_CPtr frame, const char* dst, size_t len, size_t offset);
141
142/*! @brief Helper function to finds a MMIO device frame.
143    @param paddr Physical address of the device MMIO frame.
144    @param size Size of device frame in bytes.
145    @return Path to device frame on success (gives ownership to caller), 0 otherwise.
146*/
147cspacepath_t procserv_find_device(void *paddr, int size);
148
149/*! @brief Helper Function to TLB flush on a series of frames.
150    @param frame Pointer to array of frame caps to unmap.
151    @param nFrames Number of frames in given frame array.
152*/
153void procserv_flush(seL4_CPtr *frame, int nFrames);
154
155/*! @brief Helper function to retrieve an IRQ handler for the given IRQ number. Uses a hash table
156           in order to avoid creating the same IRQ handler twice.
157    @param irq The IRQ number to create the handler for.
158    @return The corresponding IRQ handler cap if successful, 0 otherwise.
159*/
160seL4_CPtr procserv_get_irq_handler(int irq);
161
162#endif /* _REFOS_PROCESS_SERVER_STATE_H_ */
163