3 * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>
4 * economic rights: Technische Universität Dresden (Germany)
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.
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.
22 #include <l4/vcpu/vcpu.h>
27 * \brief C++ implementation of state word in the vCPU area
28 * \ingroup api_libvcpu
34 explicit State(l4vcpu_state_t v) : _s(v) {}
39 * \param bits Bits to add to the word.
41 void add(unsigned bits) throw() { _s |= bits; }
46 * \param bits Bits to clear in the word.
48 void clear(unsigned bits) throw() { _s &= ~bits; }
53 * \param v Set the word to the value of v.
55 void set(l4vcpu_state_t v) throw() { _s = v; }
62 * \brief C++ implementation of the vCPU save state area
63 * \ingroup api_libvcpu
65 class Vcpu : private l4_vcpu_state_t
68 typedef l4vcpu_irq_state_t Irq_state;
71 * \brief Disable the vCPU for event delivery.
73 void irq_disable() throw()
74 { l4vcpu_irq_disable(this); }
77 * \brief Disable the vCPU for event delivery and return previous state.
78 * \return IRQ state before disabling IRQs.
80 Irq_state irq_disable_save() throw()
81 { return l4vcpu_irq_disable_save(this); }
84 * \brief Get state word
85 * \return Pointer to state word in the vCPU
87 State *state() throw()
88 { return reinterpret_cast<State *>(&(l4_vcpu_state_t::state)); }
91 * \brief Get state word
92 * \return Pointer to state word in the vCPU
94 State state() const throw()
95 { return static_cast<State>(l4_vcpu_state_t::state); }
98 * \brief Get saved_state word
99 * \return Pointer to saved_state word in the vCPU
101 State *saved_state() throw()
102 { return reinterpret_cast<State *>(&(l4_vcpu_state_t::saved_state)); }
105 * \brief Get saved_state word
106 * \return Pointer to saved_state word in the vCPU
108 State saved_state() const throw()
109 { return static_cast<State>(l4_vcpu_state_t::saved_state); }
112 * \brief Enable the vCPU for event delivery.
114 * \param do_event_work_cb Call-back function that is called in case an
115 * event (such as an interrupt) is pending.
117 void irq_enable(l4vcpu_event_hndl_t do_event_work_cb) throw()
118 { l4vcpu_irq_enable(this, do_event_work_cb); }
121 * \brief Restore a previously saved IRQ/event state.
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.
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); }
130 void halt(l4vcpu_event_hndl_t do_event_work_cb) throw()
131 { l4vcpu_halt(this, do_event_work_cb); }
134 * \brief Set the task of the vCPU.
135 * \param task Task to set, defaults to invalid task.
137 void task(L4::Cap<L4::Task> const task = L4::Cap<L4::Task>::Invalid) throw()
138 { user_task = task.cap(); }
141 * \brief Return whether the entry reason was a page fault.
142 * return 0 if not, !=0 otherwise.
144 int is_page_fault_entry()
145 { return l4vcpu_is_page_fault_entry(this); }
148 * \brief Return whether the entry reason was an IRQ/IPC message.
149 * return 0 if not, !=0 otherwise.
152 { return l4vcpu_is_irq_entry(this); }
155 * \brief Return pointer to register state.
156 * \return Pointer to register state.
158 l4_vcpu_regs_t *r() throw()
159 { return &(l4_vcpu_state_t::r); }
162 * \brief Return pointer to register state.
163 * \return Pointer to register state.
165 l4_vcpu_regs_t const *r() const throw()
166 { return &(l4_vcpu_state_t::r); }
169 * \brief Return pointer to IPC state.
170 * \return Pointer to IPC state.
172 l4_vcpu_ipc_regs_t *i() throw()
173 { return &(l4_vcpu_state_t::i); }
176 * \brief Return pointer to IPC state.
177 * \return Pointer to IPC state.
179 l4_vcpu_ipc_regs_t const *i() const throw()
180 { return &(l4_vcpu_state_t::i); }
184 * \brief Set vCPU entry stack pointer.
185 * \param sp Stack pointer address to set.
187 * \note The value is only used when entering from a user-task.
189 void entry_sp(l4_umword_t sp)
190 { l4_vcpu_state_t::entry_sp = sp; }
193 * \brief Set vCPU entry instruction pointer.
194 * \param sp Instruction pointer address to set.
196 void entry_ip(l4_umword_t ip)
197 { l4_vcpu_state_t::entry_ip = ip; }
201 * \brief Print the state of the vCPU.
203 void print_state(const char *prefix = "") throw()
204 { l4vcpu_print_state(this, prefix); }
207 * \brief Return a vCPU given the thread UTCB pointer.
208 * \return A pointer to a vCPU.
210 static Vcpu *vcpu_from_utcb(l4_utcb_t const *utcb) throw()
211 { return reinterpret_cast<Vcpu *>((l4_umword_t)utcb + L4_UTCB_OFFSET); }