]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libvcpu/include/vcpu
update
[l4.git] / l4 / pkg / libvcpu / include / vcpu
1 // vi:se ft=cpp:
2 /*
3  * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
4  *     economic rights: Technische Universität Dresden (Germany)
5  *
6  * This file is part of TUD:OS and distributed under the terms of the
7  * GNU General Public License 2.
8  * Please see the COPYING-GPL-2 file for details.
9  *
10  * As a special exception, you may use this file as part of a free software
11  * library without restriction.  Specifically, if other files instantiate
12  * templates or use macros or inline functions from this file, or you compile
13  * this file and link it with other files to produce an executable, this
14  * file does not by itself cause the resulting executable to be covered by
15  * the GNU General Public License.  This exception does not however
16  * invalidate any other reasons why the executable file might be covered by
17  * the GNU General Public License.
18  */
19
20 #pragma once
21
22 #include <l4/vcpu/vcpu.h>
23
24 namespace L4vcpu {
25
26 /**
27  * \brief C++ implementation of state word in the vCPU area
28  * \ingroup api_libvcpu
29  */
30 class State
31 {
32 public:
33   State() {}
34   explicit State(l4vcpu_state_t v) : _s(v) {}
35
36   /**
37    * \brief Add flags.
38    *
39    * \param bits Bits to add to the word.
40    */
41   void add(unsigned bits) throw()    { _s |= bits; }
42
43   /**
44    * \brief Clear flags.
45    *
46    * \param bits Bits to clear in the word.
47    */
48   void clear(unsigned bits) throw()  { _s &= ~bits; }
49
50   /**
51    * \brief Set flags.
52    *
53    * \param v Set the word to the value of v.
54    */
55   void set(l4vcpu_state_t v) throw() { _s = v; }
56
57 private:
58   l4vcpu_state_t _s;
59 };
60
61 /**
62  * \brief C++ implementation of the vCPU save state area
63  * \ingroup api_libvcpu
64  */
65 class Vcpu : private l4_vcpu_state_t
66 {
67 public:
68   typedef l4vcpu_irq_state_t Irq_state;
69
70   /**
71    * \brief Disable the vCPU for event delivery.
72    */
73   void irq_disable() throw()
74   { l4vcpu_irq_disable(this); }
75
76   /**
77    * \brief Disable the vCPU for event delivery and return previous state.
78    * \return IRQ state before disabling IRQs.
79    */
80   Irq_state irq_disable_save() throw()
81   { return l4vcpu_irq_disable_save(this); }
82
83   /**
84    * \brief Get state word
85    * \return Pointer to state word in the vCPU
86    */
87   State *state() throw()
88   { return reinterpret_cast<State *>(&(l4_vcpu_state_t::state)); }
89
90   /**
91    * \brief Get state word
92    * \return Pointer to state word in the vCPU
93    */
94   State state() const throw()
95   { return static_cast<State>(l4_vcpu_state_t::state); }
96
97   /**
98    * \brief Get saved_state word
99    * \return Pointer to saved_state word in the vCPU
100    */
101   State *saved_state() throw()
102   { return reinterpret_cast<State *>(&(l4_vcpu_state_t::saved_state)); }
103
104   /**
105    * \brief Get saved_state word
106    * \return Pointer to saved_state word in the vCPU
107    */
108   State saved_state() const throw()
109   { return static_cast<State>(l4_vcpu_state_t::saved_state); }
110
111   /**
112    * \brief Enable the vCPU for event delivery.
113    *
114    * \param do_event_work_cb Call-back function that is called in case an
115    *                         event (such as an interrupt) is pending.
116    */
117   void irq_enable(l4vcpu_event_hndl_t do_event_work_cb) throw()
118   { l4vcpu_irq_enable(this, do_event_work_cb); }
119
120   /**
121    * \brief Restore a previously saved IRQ/event state.
122    *
123    * \param s                IRQ state to be restored.
124    * \param do_event_work_cb Call-back function that is called in case an
125    *                         event (such as an interrupt) is pending.
126    */
127   void irq_restore(Irq_state s, l4vcpu_event_hndl_t do_event_work_cb) throw()
128   { l4vcpu_irq_restore(this, s, do_event_work_cb); }
129
130   void halt(l4vcpu_event_hndl_t do_event_work_cb) throw()
131   { l4vcpu_halt(this, do_event_work_cb); }
132
133   /**
134    * \brief Set the task of the vCPU.
135    * \param task Task to set, defaults to invalid task.
136    */
137   void task(L4::Cap<L4::Task> const task = L4::Cap<L4::Task>::Invalid) throw()
138   { user_task = task.cap(); }
139
140   /**
141    * \brief Return whether the entry reason was a page fault.
142    * return 0 if not, !=0 otherwise.
143    */
144   int is_page_fault_entry()
145   { return l4vcpu_is_page_fault_entry(this); }
146
147   /**
148    * \brief Return whether the entry reason was an IRQ/IPC message.
149    * return 0 if not, !=0 otherwise.
150    */
151   int is_irq_entry()
152   { return l4vcpu_is_irq_entry(this); }
153
154   /**
155    * \brief Return pointer to register state.
156    * \return Pointer to register state.
157    */
158   l4_vcpu_regs_t *r() throw()
159   { return &(l4_vcpu_state_t::r); }
160
161   /**
162    * \brief Return pointer to register state.
163    * \return Pointer to register state.
164    */
165   l4_vcpu_regs_t const *r() const throw()
166   { return &(l4_vcpu_state_t::r); }
167
168   /**
169    * \brief Return pointer to IPC state.
170    * \return Pointer to IPC state.
171    */
172   l4_vcpu_ipc_regs_t *i() throw()
173   { return &(l4_vcpu_state_t::i); }
174
175   /**
176    * \brief Return pointer to IPC state.
177    * \return Pointer to IPC state.
178    */
179   l4_vcpu_ipc_regs_t const *i() const throw()
180   { return &(l4_vcpu_state_t::i); }
181
182
183   /**
184    * \brief Set vCPU entry stack pointer.
185    * \param sp  Stack pointer address to set.
186    *
187    * \note The value is only used when entering from a user-task.
188    */
189   void entry_sp(l4_umword_t sp)
190   { l4_vcpu_state_t::entry_sp = sp; }
191
192   /**
193    * \brief Set vCPU entry instruction pointer.
194    * \param sp  Instruction pointer address to set.
195    */
196   void entry_ip(l4_umword_t ip)
197   { l4_vcpu_state_t::entry_ip = ip; }
198
199
200   /**
201    * \brief Print the state of the vCPU.
202    */
203   void print_state(const char *prefix = "") throw()
204   { l4vcpu_print_state(this, prefix); }
205
206   /**
207    * \brief Return a vCPU given the thread UTCB pointer.
208    * \return A pointer to a vCPU.
209    */
210   static Vcpu *vcpu_from_utcb(l4_utcb_t const *utcb) throw()
211   { return reinterpret_cast<Vcpu *>((l4_umword_t)utcb + L4_UTCB_OFFSET); }
212 };
213
214
215 }