2 * Copyright 2004-2009, Haiku Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
8 /** Kernel specific structures and functions */
13 #include <SupportDefs.h>
14 #include <StorageDefs.h>
21 /* System constants */
23 #define B_OS_NAME_LENGTH 32
24 #define B_PAGE_SIZE 4096
25 #define B_INFINITE_TIMEOUT (9223372036854775807LL)
28 B_TIMEOUT
= 0x8, /* relative timeout */
29 B_RELATIVE_TIMEOUT
= 0x8, /* fails after a relative timeout
31 B_ABSOLUTE_TIMEOUT
= 0x10, /* fails after an absolute timeout
34 /* experimental Haiku only API */
35 B_TIMEOUT_REAL_TIME_BASE
= 0x40,
36 B_ABSOLUTE_REAL_TIME_TIMEOUT
= B_ABSOLUTE_TIMEOUT
37 | B_TIMEOUT_REAL_TIME_BASE
43 typedef int32 area_id
;
44 typedef int32 port_id
;
46 typedef int32 team_id
;
47 typedef int32 thread_id
;
52 typedef struct area_info
{
54 char name
[B_OS_NAME_LENGTH
];
70 #define B_CONTIGUOUS 3
71 #define B_LOMEM 4 /* B_CONTIGUOUS, < 16 MB physical address */
72 #define B_32_BIT_FULL_LOCK 5 /* B_FULL_LOCK, < 4 GB physical addresses */
73 #define B_32_BIT_CONTIGUOUS 6 /* B_CONTIGUOUS, < 4 GB physical address */
75 /* address spec for create_area(), and clone_area() */
76 #define B_ANY_ADDRESS 0
77 #define B_EXACT_ADDRESS 1
78 #define B_BASE_ADDRESS 2
79 #define B_CLONE_ADDRESS 3
80 #define B_ANY_KERNEL_ADDRESS 4
81 /* B_ANY_KERNEL_BLOCK_ADDRESS 5 */
82 #define B_RANDOMIZED_ANY_ADDRESS 6
83 #define B_RANDOMIZED_BASE_ADDRESS 7
87 #define B_WRITE_AREA 2
88 #define B_EXECUTE_AREA 4
89 #define B_STACK_AREA 8
90 /* "stack" protection is not available on most platforms - it's used
91 to only commit memory as needed, and have guard pages at the
92 bottom of the stack. */
94 extern area_id
create_area(const char *name
, void **startAddress
,
95 uint32 addressSpec
, size_t size
, uint32 lock
,
97 extern area_id
clone_area(const char *name
, void **destAddress
,
98 uint32 addressSpec
, uint32 protection
, area_id source
);
99 extern area_id
find_area(const char *name
);
100 extern area_id
area_for(void *address
);
101 extern status_t
delete_area(area_id id
);
102 extern status_t
resize_area(area_id id
, size_t newSize
);
103 extern status_t
set_area_protection(area_id id
, uint32 newProtection
);
105 /* system private, use macros instead */
106 extern status_t
_get_area_info(area_id id
, area_info
*areaInfo
, size_t size
);
107 extern status_t
_get_next_area_info(team_id team
, ssize_t
*cookie
,
108 area_info
*areaInfo
, size_t size
);
110 #define get_area_info(id, areaInfo) \
111 _get_area_info((id), (areaInfo),sizeof(*(areaInfo)))
112 #define get_next_area_info(team, cookie, areaInfo) \
113 _get_next_area_info((team), (cookie), (areaInfo), sizeof(*(areaInfo)))
118 typedef struct port_info
{
121 char name
[B_OS_NAME_LENGTH
];
122 int32 capacity
; /* queue depth */
123 int32 queue_count
; /* # msgs waiting to be read */
124 int32 total_count
; /* total # msgs read so far */
127 extern port_id
create_port(int32 capacity
, const char *name
);
128 extern port_id
find_port(const char *name
);
129 extern ssize_t
read_port(port_id port
, int32
*code
, void *buffer
,
131 extern ssize_t
read_port_etc(port_id port
, int32
*code
, void *buffer
,
132 size_t bufferSize
, uint32 flags
, bigtime_t timeout
);
133 extern status_t
write_port(port_id port
, int32 code
, const void *buffer
,
135 extern status_t
write_port_etc(port_id port
, int32 code
, const void *buffer
,
136 size_t bufferSize
, uint32 flags
, bigtime_t timeout
);
137 extern status_t
close_port(port_id port
);
138 extern status_t
delete_port(port_id port
);
140 extern ssize_t
port_buffer_size(port_id port
);
141 extern ssize_t
port_buffer_size_etc(port_id port
, uint32 flags
,
143 extern ssize_t
port_count(port_id port
);
144 extern status_t
set_port_owner(port_id port
, team_id team
);
146 /* system private, use the macros instead */
147 extern status_t
_get_port_info(port_id port
, port_info
*portInfo
,
148 size_t portInfoSize
);
149 extern status_t
_get_next_port_info(team_id team
, int32
*cookie
,
150 port_info
*portInfo
, size_t portInfoSize
);
152 #define get_port_info(port, info) \
153 _get_port_info((port), (info), sizeof(*(info)))
154 #define get_next_port_info(team, cookie, info) \
155 _get_next_port_info((team), (cookie), (info), sizeof(*(info)))
158 /* WARNING: The following is Haiku experimental API. It might be removed or
159 changed in the future. */
161 typedef struct port_message_info
{
168 /* similar to port_buffer_size_etc(), but returns (more) info */
169 extern status_t
_get_port_message_info_etc(port_id port
,
170 port_message_info
*info
, size_t infoSize
, uint32 flags
,
173 #define get_port_message_info_etc(port, info, flags, timeout) \
174 _get_port_message_info_etc((port), (info), sizeof(*(info)), flags, timeout)
179 typedef struct sem_info
{
182 char name
[B_OS_NAME_LENGTH
];
184 thread_id latest_holder
;
187 /* semaphore flags */
189 B_CAN_INTERRUPT
= 0x01, /* acquisition of the semaphore can be
190 interrupted (system use only) */
191 B_CHECK_PERMISSION
= 0x04, /* ownership will be checked (system use
193 B_KILL_CAN_INTERRUPT
= 0x20, /* acquisition of the semaphore can be
194 interrupted by SIGKILL[THR], even
195 if not B_CAN_INTERRUPT (system use
198 /* release_sem_etc() only flags */
199 B_DO_NOT_RESCHEDULE
= 0x02, /* thread is not rescheduled */
200 B_RELEASE_ALL
= 0x08, /* all waiting threads will be woken up,
201 count will be zeroed */
202 B_RELEASE_IF_WAITING_ONLY
= 0x10 /* release count only if there are any
206 extern sem_id
create_sem(int32 count
, const char *name
);
207 extern status_t
delete_sem(sem_id id
);
208 extern status_t
acquire_sem(sem_id id
);
209 extern status_t
acquire_sem_etc(sem_id id
, int32 count
, uint32 flags
,
211 extern status_t
release_sem(sem_id id
);
212 extern status_t
release_sem_etc(sem_id id
, int32 count
, uint32 flags
);
213 /* TODO: the following two calls are not part of the BeOS API, and might be
214 changed or even removed for the final release of Haiku R1 */
215 extern status_t
switch_sem(sem_id semToBeReleased
, sem_id id
);
216 extern status_t
switch_sem_etc(sem_id semToBeReleased
, sem_id id
,
217 int32 count
, uint32 flags
, bigtime_t timeout
);
218 extern status_t
get_sem_count(sem_id id
, int32
*threadCount
);
219 extern status_t
set_sem_owner(sem_id id
, team_id team
);
221 /* system private, use the macros instead */
222 extern status_t
_get_sem_info(sem_id id
, struct sem_info
*info
,
224 extern status_t
_get_next_sem_info(team_id team
, int32
*cookie
,
225 struct sem_info
*info
, size_t infoSize
);
227 #define get_sem_info(sem, info) \
228 _get_sem_info((sem), (info), sizeof(*(info)))
230 #define get_next_sem_info(team, cookie, info) \
231 _get_next_sem_info((team), (cookie), (info), sizeof(*(info)))
241 thread_id debugger_nub_thread
;
242 port_id debugger_nub_port
;
249 #define B_CURRENT_TEAM 0
250 #define B_SYSTEM_TEAM 1
252 extern status_t
kill_team(team_id team
);
253 /* see also: send_signal() */
255 /* system private, use macros instead */
256 extern status_t
_get_team_info(team_id id
, team_info
*info
, size_t size
);
257 extern status_t
_get_next_team_info(int32
*cookie
, team_info
*info
,
260 #define get_team_info(id, info) \
261 _get_team_info((id), (info), sizeof(*(info)))
263 #define get_next_team_info(cookie, info) \
264 _get_next_team_info((cookie), (info), sizeof(*(info)))
266 /* team usage info */
270 bigtime_t kernel_time
;
274 /* compatible to sys/resource.h RUSAGE_SELF and RUSAGE_CHILDREN */
275 B_TEAM_USAGE_SELF
= 0,
276 B_TEAM_USAGE_CHILDREN
= -1
279 /* system private, use macros instead */
280 extern status_t
_get_team_usage_info(team_id team
, int32 who
,
281 team_usage_info
*info
, size_t size
);
283 #define get_team_usage_info(team, who, info) \
284 _get_team_usage_info((team), (who), (info), sizeof(*(info)))
290 B_THREAD_RUNNING
= 1,
301 char name
[B_OS_NAME_LENGTH
];
306 bigtime_t kernel_time
;
311 #define B_IDLE_PRIORITY 0
312 #define B_LOWEST_ACTIVE_PRIORITY 1
313 #define B_LOW_PRIORITY 5
314 #define B_NORMAL_PRIORITY 10
315 #define B_DISPLAY_PRIORITY 15
316 #define B_URGENT_DISPLAY_PRIORITY 20
317 #define B_REAL_TIME_DISPLAY_PRIORITY 100
318 #define B_URGENT_PRIORITY 110
319 #define B_REAL_TIME_PRIORITY 120
321 #define B_SYSTEM_TIMEBASE 0
322 /* time base for snooze_*(), compatible with the clockid_t constants defined
325 #define B_FIRST_REAL_TIME_PRIORITY B_REAL_TIME_DISPLAY_PRIORITY
327 typedef status_t (*thread_func
)(void *);
328 #define thread_entry thread_func
329 /* thread_entry is for backward compatibility only! Use thread_func */
331 extern thread_id
spawn_thread(thread_func
, const char *name
, int32 priority
,
333 extern status_t
kill_thread(thread_id thread
);
334 extern status_t
resume_thread(thread_id thread
);
335 extern status_t
suspend_thread(thread_id thread
);
337 extern status_t
rename_thread(thread_id thread
, const char *newName
);
338 extern status_t
set_thread_priority(thread_id thread
, int32 newPriority
);
339 extern void exit_thread(status_t status
);
340 extern status_t
wait_for_thread(thread_id thread
, status_t
*returnValue
);
341 extern status_t
on_exit_thread(void (*callback
)(void *), void *data
);
343 extern thread_id
find_thread(const char *name
);
345 extern status_t
send_data(thread_id thread
, int32 code
, const void *buffer
,
347 extern int32
receive_data(thread_id
*sender
, void *buffer
,
349 extern bool has_data(thread_id thread
);
351 extern status_t
snooze(bigtime_t amount
);
352 extern status_t
snooze_etc(bigtime_t amount
, int timeBase
, uint32 flags
);
353 extern status_t
snooze_until(bigtime_t time
, int timeBase
);
355 /* system private, use macros instead */
356 extern status_t
_get_thread_info(thread_id id
, thread_info
*info
, size_t size
);
357 extern status_t
_get_next_thread_info(team_id team
, int32
*cookie
,
358 thread_info
*info
, size_t size
);
360 #define get_thread_info(id, info) \
361 _get_thread_info((id), (info), sizeof(*(info)))
363 #define get_next_thread_info(team, cookie, info) \
364 _get_next_thread_info((team), (cookie), (info), sizeof(*(info)))
366 /* bridge to the pthread API */
367 extern thread_id
get_pthread_thread_id(pthread_t thread
);
368 /* TODO: Would be nice to have, but we use TLS to associate a thread with its
369 pthread object. So this is not trivial to implement.
370 extern status_t convert_to_pthread(thread_id thread, pthread_t *_thread);
376 extern unsigned long real_time_clock(void);
377 extern void set_real_time_clock(unsigned long secsSinceJan1st1970
);
378 extern bigtime_t
real_time_clock_usecs(void);
379 extern bigtime_t
system_time(void);
380 /* time since booting in microseconds */
381 extern nanotime_t
system_time_nsecs(void);
382 /* time since booting in nanoseconds */
384 /* deprecated (is no-op) */
385 extern status_t
set_timezone(const char *timezone
);
390 B_ONE_SHOT_ABSOLUTE_ALARM
= 1,
391 B_ONE_SHOT_RELATIVE_ALARM
,
392 B_PERIODIC_ALARM
/* "when" specifies the period */
395 extern bigtime_t
set_alarm(bigtime_t when
, uint32 flags
);
400 extern void debugger(const char *message
);
403 calling this function with a non-zero value will cause your thread
404 to receive signals for any exceptional conditions that occur (i.e.
405 you'll get SIGSEGV for data access exceptions, SIGFPE for floating
406 point errors, SIGILL for illegal instructions, etc).
408 to re-enable the default debugger pass a zero.
410 extern int disable_debugger(int state
);
412 /* TODO: Remove. Temporary debug helper. */
413 extern void debug_printf(const char *format
, ...)
414 __attribute__ ((format (__printf__
, 1, 2)));
415 extern void debug_vprintf(const char *format
, va_list args
);
416 extern void ktrace_printf(const char *format
, ...)
417 __attribute__ ((format (__printf__
, 1, 2)));
418 extern void ktrace_vprintf(const char *format
, va_list args
);
421 /* System information */
424 bigtime_t active_time
; /* usec of doing useful work since boot */
429 bigtime_t boot_time
; /* time of boot (usecs since 1/1/1970) */
431 uint32 cpu_count
; /* number of cpus */
433 uint64 max_pages
; /* total # of accessible pages */
434 uint64 used_pages
; /* # of accessible pages in use */
436 uint64 block_cache_pages
;
437 uint64 ignored_pages
; /* # of ignored/inaccessible pages */
439 uint64 needed_memory
;
442 uint64 max_swap_pages
;
443 uint64 free_swap_pages
;
445 uint32 page_faults
; /* # of page faults */
459 char kernel_name
[B_FILE_NAME_LENGTH
];
460 char kernel_build_date
[B_OS_NAME_LENGTH
];
461 char kernel_build_time
[B_OS_NAME_LENGTH
];
463 int64 kernel_version
;
464 uint32 abi
; /* the system API */
467 enum topology_level_type
{
490 B_CPU_VENDOR_UNKNOWN
,
495 B_CPU_VENDOR_NATIONAL_SEMICONDUCTOR
,
497 B_CPU_VENDOR_TRANSMETA
,
500 B_CPU_VENDOR_MOTOROLA
,
505 enum cpu_platform platform
;
506 } cpu_topology_root_info
;
509 enum cpu_vendor vendor
;
510 uint32 cache_line_size
;
511 } cpu_topology_package_info
;
515 uint64 default_frequency
;
516 } cpu_topology_core_info
;
520 enum topology_level_type type
;
524 cpu_topology_root_info root
;
525 cpu_topology_package_info package
;
526 cpu_topology_core_info core
;
528 } cpu_topology_node_info
;
531 extern status_t
get_system_info(system_info
* info
);
532 extern status_t
get_cpu_info(uint32 firstCPU
, uint32 cpuCount
,
534 extern status_t
get_cpu_topology_info(cpu_topology_node_info
* topologyInfos
,
535 uint32
* topologyInfoCount
);
537 #if defined(__INTEL__) || defined(__x86_64__)
549 uint32 reserved_0
: 2;
550 uint32 extended_model
: 4;
551 uint32 extended_family
: 8;
552 uint32 reserved_1
: 4;
554 uint32 brand_index
: 8;
556 uint32 logical_cpus
: 8;
560 uint32 extended_features
;
565 uint8 cache_descriptors
[15];
570 uint32 serial_number_high
;
571 uint32 serial_number_low
;
584 extern status_t
get_cpuid(cpuid_info
*info
, uint32 eaxRegister
,
589 extern int32
is_computer_on(void);
590 extern double is_computer_on_fire(void);
593 /* signal related functions */
594 int send_signal(thread_id threadID
, unsigned int signal
);
595 void set_signal_stack(void* base
, size_t size
);
598 /* WARNING: Experimental API! */
601 B_OBJECT_TYPE_FD
= 0,
602 B_OBJECT_TYPE_SEMAPHORE
= 1,
603 B_OBJECT_TYPE_PORT
= 2,
604 B_OBJECT_TYPE_THREAD
= 3
608 B_EVENT_READ
= 0x0001, /* FD/port readable */
609 B_EVENT_WRITE
= 0x0002, /* FD/port writable */
610 B_EVENT_ERROR
= 0x0004, /* FD error */
611 B_EVENT_PRIORITY_READ
= 0x0008, /* FD priority readable */
612 B_EVENT_PRIORITY_WRITE
= 0x0010, /* FD priority writable */
613 B_EVENT_HIGH_PRIORITY_READ
= 0x0020, /* FD high priority readable */
614 B_EVENT_HIGH_PRIORITY_WRITE
= 0x0040, /* FD high priority writable */
615 B_EVENT_DISCONNECTED
= 0x0080, /* FD disconnected */
617 B_EVENT_ACQUIRE_SEMAPHORE
= 0x0001, /* semaphore can be acquired */
619 B_EVENT_INVALID
= 0x1000 /* FD/port/sem/thread ID not or
620 no longer valid (e.g. has been
624 typedef struct object_wait_info
{
625 int32 object
; /* ID of the object */
626 uint16 type
; /* type of the object */
627 uint16 events
; /* events mask */
630 /* wait_for_objects[_etc]() waits until at least one of the specified events or,
631 if given, the timeout occurred. When entering the function the
632 object_wait_info::events field specifies the events for each object the
633 caller is interested in. When the function returns the fields reflect the
634 events that actually occurred. The events B_EVENT_INVALID, B_EVENT_ERROR,
635 and B_EVENT_DISCONNECTED don't need to be specified. They will always be
636 reported, when they occur. */
638 extern ssize_t
wait_for_objects(object_wait_info
* infos
, int numInfos
);
639 extern ssize_t
wait_for_objects_etc(object_wait_info
* infos
, int numInfos
,
640 uint32 flags
, bigtime_t timeout
);