ia64/xen-unstable

view extras/mini-os/include/ia64/os.h @ 16838:945820bfedb6

minios: POSIX fixes
Fixes some functions which are POSIX. Also make them ifndef HAVE_LIBC.

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Jan 22 14:20:22 2008 +0000 (2008-01-22)
parents 03c2236e0089
children e35a379e7fe9
line source
1 /*
2 * Copyright (C) 2007 - Dietmar Hahn <dietmar.hahn@fujitsu-siemens.com>
3 *
4 ****************************************************************************
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
25 #if !defined(__OS_H__)
26 #define __OS_H__
28 #if !defined(__ASSEMBLY__)
30 #include "types.h"
31 #include "endian.h"
32 #include "ia64_cpu.h"
33 #include "atomic.h"
34 #include "efi.h"
35 #include "sal.h"
36 #include "pal.h"
37 #include "hypervisor.h"
40 typedef uint64_t paddr_t; /* Physical address. */
41 #ifndef HAVE_LIBC
42 typedef uint64_t caddr_t; /* rr7/kernel memory address. */
43 #endif
45 #include "page.h"
46 #include "mm.h"
49 void do_exit(void) __attribute__((noreturn));
50 void arch_init(start_info_t *si); /* in common.c */
51 void arch_print_info(void); /* in common.c */
54 /* Size of xen_ia64_boot_param.command_line */
55 #define COMMAND_LINE_SIZE 512
57 extern struct xen_ia64_boot_param* ia64_boot_paramP;
58 extern struct xen_ia64_boot_param ia64BootParamG;
59 extern char boot_cmd_line[];
60 extern efi_system_table_t* efiSysTableP;
61 extern int bootverbose;
63 extern void ia64_probe_sapics(void);
67 /* Contains the needed stuff from efi. */
68 struct efi
69 {
71 efi_system_table_t* efiSysTableP;
72 efi_set_virtual_address_map_t setVirtAddrMapF;
73 efi_get_time_t getTimeF;
74 efi_reset_system_t resetSystemF;
76 };
78 struct machine_fw
79 {
80 struct efi efi;
82 uint64_t ia64_port_base; /* physical address */
83 uint64_t ia64_pal_base; /* virtual rr7 address */
85 sal_system_table_t* ia64_sal_tableP;
86 sal_entry_t* ia64_sal_entryP; /* SAL_PROC entrypoint */
88 uint64_t ia64_efi_acpi_table; /* physical address */
89 uint64_t ia64_efi_acpi20_table; /* physical address */
91 uint64_t mach_mem_start; /* phys start addr of machine memory */
92 uint64_t mach_mem_size; /* size of machine memory */
94 uint64_t kernstart; /* virt address of kern text start */
95 uint64_t kernend;
96 uint64_t kernpstart; /* phys address of kern text start */
97 uint64_t kernpend;
98 };
100 extern struct machine_fw machineFwG;
102 #define ia64_sal_entry machineFwG.ia64_sal_entryP
104 #define smp_processor_id() 0
106 static inline uint64_t
107 xchg8(uint64_t* ptr, uint64_t x) \
108 {
109 uint64_t oldVal;
110 asm volatile ("xchg8 %0=[%1],%2" : "=r" (oldVal)
111 : "r" (ptr), "r" (x) : "memory");
112 return oldVal;
113 }
114 #define xchg xchg8
116 // Counts the number of 1-bits in x.
117 #if __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
118 # define get_popcnt(x) __builtin_popcountl(x)
119 #else
120 # define get_popcnt(x) \
121 ({ \
122 uint64_t num; \
123 asm ("popcnt %0=%1" : "=r" (num) : "r" (x)); \
124 num; \
125 })
126 #endif
128 /**
129 * __ffs - find first bit in word.
130 * @x: The word to search
131 *
132 * Undefined if no bit exists, so code should check against 0 first.
133 */
134 static inline unsigned long
135 __ffs (unsigned long x)
136 {
137 unsigned long result;
139 result = get_popcnt((x-1) & ~x);
140 return result;
141 }
144 static inline void
145 synch_clear_bit(int num, volatile void *addr)
146 {
147 clear_bit(num, addr);
148 }
150 static inline void
151 synch_set_bit(int num, volatile void *addr)
152 {
153 set_bit(num, addr);
154 }
156 static inline int
157 synch_test_bit(int nr, const volatile void *addr)
158 {
159 return test_bit(nr, addr);
160 }
162 static inline int
163 synch_test_and_set_bit(int num, volatile void * addr)
164 {
165 return test_and_set_bit(num, addr);
166 }
169 #define synch_cmpxchg(ptr, old, new) \
170 ((__typeof__(*(ptr)))__synch_cmpxchg((ptr),\
171 (unsigned long)(old), \
172 (unsigned long)(new), \
173 sizeof(*(ptr))))
175 static inline unsigned long
176 __synch_cmpxchg(volatile void *ptr, uint64_t old, uint64_t new, int size)
177 {
178 switch (size)
179 {
180 case 1:
181 return ia64_cmpxchg_acq_8(ptr, old, new);
182 case 2:
183 return ia64_cmpxchg_acq_16(ptr, old, new);
184 case 4:
185 return ia64_cmpxchg_acq_32(ptr, old, new);
186 case 8:
187 return ia64_cmpxchg_acq_64(ptr, old, new);
188 }
189 return ia64_cmpxchg_acq_64(ptr, old, new);
190 }
192 /*
193 * Force a proper event-channel callback from Xen after clearing the
194 * callback mask. We do this in a very simple manner, by making a call
195 * down into Xen. The pending flag will be checked by Xen on return.
196 */
197 static inline void
198 force_evtchn_callback(void)
199 {
200 (void)HYPERVISOR_xen_version(0, NULL);
201 }
203 extern shared_info_t *HYPERVISOR_shared_info;
205 static inline int
206 HYPERVISOR_shutdown(unsigned int reason)
207 {
208 struct sched_shutdown sched_shutdown = {
209 .reason = reason
210 };
212 int rc = HYPERVISOR_sched_op(SCHEDOP_shutdown, &sched_shutdown);
214 if (rc == -ENOSYS)
215 rc = HYPERVISOR_sched_op_compat(SCHEDOP_shutdown, reason);
217 return rc;
218 }
221 /*
222 * This code is from the originally os.h and should be put in a
223 * common header file!
224 */
226 /*
227 * The use of 'barrier' in the following reflects their use as local-lock
228 * operations. Reentrancy must be prevented (e.g., __cli()) /before/ following
229 * critical operations are executed. All critical operations must complete
230 * /before/ reentrancy is permitted (e.g., __sti()). Alpha architecture also
231 * includes these barriers, for example.
232 */
234 #define __cli() \
235 do { \
236 vcpu_info_t *_vcpu; \
237 _vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()]; \
238 _vcpu->evtchn_upcall_mask = SWAP(1); \
239 barrier(); \
240 } while (0)
242 #define __sti() \
243 do { \
244 vcpu_info_t *_vcpu; \
245 barrier(); \
246 _vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()]; \
247 _vcpu->evtchn_upcall_mask = 0; \
248 barrier(); /* unmask then check (avoid races) */ \
249 if (unlikely(SWAP(_vcpu->evtchn_upcall_pending))) \
250 force_evtchn_callback(); \
251 } while (0)
253 #define __save_flags(x) \
254 do { \
255 vcpu_info_t *_vcpu; \
256 _vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()]; \
257 (x) = SWAP(_vcpu->evtchn_upcall_mask); \
258 } while (0)
260 #define __restore_flags(x) \
261 do { \
262 vcpu_info_t *_vcpu; \
263 barrier(); \
264 _vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()]; \
265 if ((_vcpu->evtchn_upcall_mask = (x)) == 0) { \
266 barrier(); /* unmask then check (avoid races) */ \
267 if ( unlikely(SWAP(_vcpu->evtchn_upcall_pending)) ) \
268 force_evtchn_callback(); \
269 }\
270 } while (0)
272 #define safe_halt() ((void)0)
274 #define __save_and_cli(x) \
275 do { \
276 vcpu_info_t *_vcpu; \
277 _vcpu = &HYPERVISOR_shared_info->vcpu_info[smp_processor_id()]; \
278 (x) = SWAP(_vcpu->evtchn_upcall_mask); \
279 _vcpu->evtchn_upcall_mask = SWAP(1); \
280 barrier(); \
281 } while (0)
283 #define local_irq_save(x) __save_and_cli(x)
284 #define local_irq_restore(x) __restore_flags(x)
285 #define local_save_flags(x) __save_flags(x)
286 #define local_irq_disable() __cli()
287 #define local_irq_enable() __sti()
289 #define irqs_disabled() \
290 SWAP(HYPERVISOR_shared_info->vcpu_info[smp_processor_id()].evtchn_upcall_mask)
292 /* This is a barrier for the compiler only, NOT the processor! */
293 #define barrier() __asm__ __volatile__("": : :"memory")
295 #define mb() ia64_mf()
296 #define rmb() mb()
297 #define wmb() mb()
300 #define BUG() \
301 { printk("mini-os BUG at %s:%d!\n", __FILE__, __LINE__); do_exit(); }
303 #define PRINT_BV(_fmt, _params...) \
304 if (bootverbose) \
305 printk(_fmt , ## _params)
307 #endif /* !defined(__ASSEMBLY__) */
309 #if defined(__ASSEMBLY__)
311 #define UL_CONST(x) x
312 #define UL_TYPE(x) x
314 #else /* defined(__ASSEMBLY__) */
316 #define UL_CONST(x) x##UL
317 #define UL_TYPE(x) ((uint64_t)x)
319 #endif /* defined(__ASSEMBLY__) */
321 #endif /* !defined(__OS_H__) */