1/**
2 * \file
3 * \brief The API to lib/barrelfish/dispatch
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#ifndef LIBBARRELFISH_DISPATCH_H
16#define LIBBARRELFISH_DISPATCH_H
17
18/// Stack size for dispatcher, in words
19#define DISPATCHER_STACK_BYTES  (8192)     /* 8kB */
20#define DISPATCHER_STACK_WORDS  (DISPATCHER_STACK_BYTES / sizeof(uintptr_t))
21
22#ifndef __ASSEMBLER__
23
24#include <sys/cdefs.h>
25#include <barrelfish_kpi/dispatcher_handle.h>
26#include <barrelfish_kpi/registers_arch.h>
27#include <barrelfish_kpi/types.h>
28
29__BEGIN_DECLS
30
31#ifdef  NDEBUG
32#define assert_disabled(e)      ((void)sizeof(e))
33#define warn_disabled(v,e)      ((void)sizeof(e))
34#else
35
36// Convert preprocessor macro to string (as recommended by CPP info manual)
37#define __xstr(s)       __str(s)
38#define __str(s)        #s
39
40#define assert_disabled(e) \
41    ((e) ? (void)0 : disp_assert_fail(#e, __FILE__, __func__, __xstr(__LINE__)))
42#define warn_disabled(v,e)						\
43    (((*(v))||(e)) ? (void)0 : (((*(v))=true),disp_warn_fail(#e, __FILE__, __func__, __xstr(__LINE__))))
44#endif
45
46void disp_init_disabled(dispatcher_handle_t handle);
47int disp_init_onthread(void);
48
49dispatcher_handle_t disp_disable(void);
50dispatcher_handle_t disp_try_disable(bool *was_enabled);
51void disp_enable(dispatcher_handle_t handle);
52
53
54void disp_arch_init(dispatcher_handle_t handle);
55
56/**
57 * \brief Resume execution of a given register state
58 *
59 * This function resumes the execution of the given register state on the
60 * current dispatcher. It may only be called while the dispatcher is disabled.
61 *
62 * \param disp Current dispatcher pointer
63 * \param regs Register state snapshot
64 */
65void disp_resume(dispatcher_handle_t handle, arch_registers_state_t *archregs);
66
67/**
68 * \brief Switch execution between two register states, and turn off
69 * disabled activations.
70 *
71 * This function saves as much as necessary of the current register state
72 * (which, when resumed will return to the caller), and switches execution
73 * by resuming the given register state.  It may only be called while the
74 * dispatcher is disabled.  A side effect is that activations are reenabled.
75 * Note that the thread context saved is a voluntary save so only callee
76 * save registers need to be saved, but we dont currently provide any
77 * way to optimise the corresponding resume.
78 *
79 * \param disp Current dispatcher pointer
80 * \param from_regs Location to save current register state
81 * \param to_regs Location from which to resume new register state
82 */
83void disp_switch(dispatcher_handle_t handle, arch_registers_state_t *from_state,
84                 arch_registers_state_t *to_state);
85
86/**
87 * \brief Save the current register state and optionally yield the CPU
88 *
89 * This function saves as much as necessary of the current register state
90 * (which, when resumed will return to the caller), and then either
91 * re-enters the thread scheduler or yields the CPU.
92 * It may only be called while the dispatcher is disabled.
93 * Note that the thread context saved is a voluntary save so only callee
94 * save registers need to be saved, but we dont currently provide any
95 * way to optimise the corresponding resume.
96 *
97 * \param disp Current dispatcher pointer
98 * \param regs Location to save current register state
99 * \param yield If true, yield CPU to kernel; otherwise re-run thread scheduler
100 * \param yield_to Endpoint capability for dispatcher to which we want to yield
101 */
102void disp_save(dispatcher_handle_t handle, arch_registers_state_t *state,
103               bool yield, capaddr_t yield_to);
104
105void disp_save_suspend(void);
106
107void disp_save_rm_kcb(void);
108
109void __attribute__((noreturn)) disp_yield_disabled(dispatcher_handle_t handle);
110dispatcher_handle_t disp_new(int core_id);
111
112const char *disp_name(void);
113
114#ifdef __k1om__
115uint8_t disp_xeon_phi_id(void);
116#endif
117uint64_t disp_run_counter(void);
118
119void disp_assert_fail(const char *exp, const char *file, const char *func,
120                      const char *line) __attribute((noreturn));
121
122void disp_warn_fail(const char *exp, const char *file, const char *func,
123		    const char *line);
124
125__END_DECLS
126
127#endif //__ASSEMBLER__
128
129#endif
130