1/*
2 * Copyright 2014, General Dynamics C4 Systems
3 *
4 * This software may be distributed and modified according to the terms of
5 * the GNU General Public License version 2. Note that NO WARRANTY is provided.
6 * See "LICENSE_GPLv2.txt" for details.
7 *
8 * @TAG(GD_GPL)
9 */
10
11#ifndef __API_TYPES_H
12#define __API_TYPES_H
13
14#include <config.h>
15#include <stdint.h>
16#include <util.h>
17#include <mode/api/shared_types_gen.h>
18#include <arch/api/types.h>
19#include <arch/types.h>
20#include <api/macros.h>
21#include <api/constants.h>
22#include <api/shared_types.h>
23#include <machine/io.h>
24
25/* seL4_CapRights_t defined in mode/api/shared_types.bf */
26
27typedef word_t prio_t;
28
29enum domainConstants {
30    minDom = 0,
31    maxDom = CONFIG_NUM_DOMAINS - 1
32};
33
34struct cap_transfer {
35    cptr_t ctReceiveRoot;
36    cptr_t ctReceiveIndex;
37    word_t ctReceiveDepth;
38};
39typedef struct cap_transfer cap_transfer_t;
40
41enum ctLimits {
42    capTransferDataSize = 3
43};
44
45static inline seL4_CapRights_t CONST
46rightsFromWord(word_t w)
47{
48    seL4_CapRights_t seL4_CapRights;
49
50    seL4_CapRights.words[0] = w;
51    return seL4_CapRights;
52}
53
54static inline word_t CONST
55wordFromRights(seL4_CapRights_t seL4_CapRights)
56{
57    return seL4_CapRights.words[0] & MASK(3);
58}
59
60static inline cap_transfer_t PURE
61capTransferFromWords(word_t *wptr)
62{
63    cap_transfer_t transfer;
64
65    transfer.ctReceiveRoot  = (cptr_t)wptr[0];
66    transfer.ctReceiveIndex = (cptr_t)wptr[1];
67    transfer.ctReceiveDepth = wptr[2];
68    return transfer;
69}
70
71static inline seL4_MessageInfo_t CONST
72messageInfoFromWord_raw(word_t w)
73{
74    seL4_MessageInfo_t mi;
75
76    mi.words[0] = w;
77    return mi;
78}
79
80static inline seL4_MessageInfo_t CONST
81messageInfoFromWord(word_t w)
82{
83    seL4_MessageInfo_t mi;
84    word_t len;
85
86    mi.words[0] = w;
87
88    len = seL4_MessageInfo_get_length(mi);
89    if (len > seL4_MsgMaxLength) {
90        mi = seL4_MessageInfo_set_length(mi, seL4_MsgMaxLength);
91    }
92
93    return mi;
94}
95
96static inline word_t CONST
97wordFromMessageInfo(seL4_MessageInfo_t mi)
98{
99    return mi.words[0];
100}
101
102#ifdef CONFIG_PRINTING
103#ifdef CONFIG_COLOUR_PRINTING
104#define ANSI_RESET "\033[0m"
105#define ANSI_GREEN ANSI_RESET "\033[32m"
106#define ANSI_DARK  ANSI_RESET "\033[30;1m"
107#else
108#define ANSI_RESET ""
109#define ANSI_GREEN ANSI_RESET ""
110#define ANSI_DARK  ANSI_RESET ""
111#endif
112
113/*
114 * thread name is only available if the kernel is built in debug mode.
115 */
116#ifdef CONFIG_DEBUG_BUILD
117#define THREAD_NAME NODE_STATE(ksCurThread)->tcbName
118#else
119#define THREAD_NAME ""
120#endif
121
122/*
123 * Print to serial a message helping userspace programmers to determine why the
124 * kernel is not performing their requested operation.
125 */
126#define userError(...) \
127    do {                                                                     \
128        printf(ANSI_DARK "<<" ANSI_GREEN "seL4(CPU %lu)" ANSI_DARK           \
129                " [%s/%d T%p \"%s\" @%lx]: ",                                \
130                SMP_TERNARY(getCurrentCPUIndex(), 0lu),                      \
131                __func__, __LINE__, NODE_STATE(ksCurThread),                 \
132                THREAD_NAME,                                                 \
133                (word_t)getRestartPC(NODE_STATE(ksCurThread)));              \
134        printf(__VA_ARGS__);                                                 \
135        printf(">>" ANSI_RESET "\n");                                        \
136    } while (0)
137#else /* !CONFIG_PRINTING */
138#define userError(...)
139#endif
140
141#endif
142