17af3465eSAxel Dörfler/*
2f1fafe31SAugustin Cavalier * Copyright 2004-2019, Haiku, Inc. All rights reserved.
340bfa3f7SAxel Dörfler * Distributed under the terms of the MIT License.
440bfa3f7SAxel Dörfler */
552a38012Sejakowatz#ifndef _OS_H
652a38012Sejakowatz#define _OS_H
77ba272b8SAxel Dörfler
897dfeb96SAdrien Destugues/** Kernel specific structures and functions */
9c69aed7aSAxel Dörfler
1057b6c3bcSIngo Weinhold#include <stdarg.h>
1169712d5cSAugustin Cavalier#include <sys/types.h>
1257b6c3bcSIngo Weinhold
13d7e489f8Sbeveloper#include <SupportDefs.h>
14d7e489f8Sbeveloper#include <StorageDefs.h>
153f6f3d29SAxel Dörfler
16c321cf49SAxel Dörfler
1752a38012Sejakowatz#ifdef __cplusplus
1852a38012Sejakowatzextern "C" {
21c321cf49SAxel Dörfler/* System constants */
22c321cf49SAxel Dörfler
23c321cf49SAxel Dörfler#define B_OS_NAME_LENGTH	32
24c321cf49SAxel Dörfler#define B_PAGE_SIZE			4096
25c321cf49SAxel Dörfler#define B_INFINITE_TIMEOUT	(9223372036854775807LL)
26c321cf49SAxel Dörfler
27c321cf49SAxel Dörflerenum {
284d3680aaSIngo Weinhold	B_TIMEOUT						= 0x8,	/* relative timeout */
294d3680aaSIngo Weinhold	B_RELATIVE_TIMEOUT				= 0x8,	/* fails after a relative timeout
304d3680aaSIngo Weinhold												with B_TIMED_OUT */
314d3680aaSIngo Weinhold	B_ABSOLUTE_TIMEOUT				= 0x10,	/* fails after an absolute timeout
324d3680aaSIngo Weinhold												with B_TIMED_OUT */
334d3680aaSIngo Weinhold
344d3680aaSIngo Weinhold	/* experimental Haiku only API */
354d3680aaSIngo Weinhold	B_TIMEOUT_REAL_TIME_BASE		= 0x40,
374d3680aaSIngo Weinhold										| B_TIMEOUT_REAL_TIME_BASE
38c321cf49SAxel Dörfler};
39c321cf49SAxel Dörfler
407af3465eSAxel Dörfler
41c321cf49SAxel Dörfler/* Types */
42c321cf49SAxel Dörfler
43c321cf49SAxel Dörflertypedef int32 area_id;
44c321cf49SAxel Dörflertypedef int32 port_id;
45c321cf49SAxel Dörflertypedef int32 sem_id;
46c321cf49SAxel Dörflertypedef int32 team_id;
47c321cf49SAxel Dörflertypedef int32 thread_id;
48db73233cSDavid Reid
49c321cf49SAxel Dörfler
5052a38012Sejakowatz/* Areas */
51c321cf49SAxel Dörfler
5252a38012Sejakowatztypedef struct area_info {
5352a38012Sejakowatz	area_id		area;
5452a38012Sejakowatz	char		name[B_OS_NAME_LENGTH];
5552a38012Sejakowatz	size_t		size;
5652a38012Sejakowatz	uint32		lock;
5752a38012Sejakowatz	uint32		protection;
5852a38012Sejakowatz	team_id		team;
5952a38012Sejakowatz	uint32		ram_size;
6052a38012Sejakowatz	uint32		copy_count;
6152a38012Sejakowatz	uint32		in_count;
6252a38012Sejakowatz	uint32		out_count;
6352a38012Sejakowatz	void		*address;
6452a38012Sejakowatz} area_info;
66c321cf49SAxel Dörfler/* area locking */
672c1e463cSAxel Dörfler#define B_NO_LOCK				0
682c1e463cSAxel Dörfler#define B_LAZY_LOCK				1
692c1e463cSAxel Dörfler#define B_FULL_LOCK				2
702c1e463cSAxel Dörfler#define B_CONTIGUOUS			3
71be87d0a0SIngo Weinhold#define	B_LOMEM					4	/* B_CONTIGUOUS, < 16 MB physical address */
729fb2d737SIngo Weinhold#define	B_32_BIT_FULL_LOCK		5	/* B_FULL_LOCK, < 4 GB physical addresses */
739fb2d737SIngo Weinhold#define	B_32_BIT_CONTIGUOUS		6	/* B_CONTIGUOUS, < 4 GB physical address */
74c321cf49SAxel Dörfler
75c321cf49SAxel Dörfler/* address spec for create_area(), and clone_area() */
76f9bab525SPawel Dziepak#define B_ANY_ADDRESS				0
77f9bab525SPawel Dziepak#define B_EXACT_ADDRESS				1
78f9bab525SPawel Dziepak#define B_BASE_ADDRESS				2
79f9bab525SPawel Dziepak#define B_CLONE_ADDRESS				3
80f9bab525SPawel Dziepak#define	B_ANY_KERNEL_ADDRESS		4
81f9bab525SPawel Dziepak/* B_ANY_KERNEL_BLOCK_ADDRESS		5 */
82b3e4c677SPawel Dziepak#define B_RANDOMIZED_ANY_ADDRESS	6
83f9bab525SPawel Dziepak#define B_RANDOMIZED_BASE_ADDRESS	7
84c321cf49SAxel Dörfler
85c321cf49SAxel Dörfler/* area protection */
868a0c9d52SAugustin Cavalier#define B_READ_AREA				(1 << 0)
878a0c9d52SAugustin Cavalier#define B_WRITE_AREA			(1 << 1)
888a0c9d52SAugustin Cavalier#define B_EXECUTE_AREA			(1 << 2)
898a0c9d52SAugustin Cavalier#define B_STACK_AREA			(1 << 3)
9097dfeb96SAdrien Destugues	/* "stack" protection is not available on most platforms - it's used
9197dfeb96SAdrien Destugues	   to only commit memory as needed, and have guard pages at the
9297dfeb96SAdrien Destugues	   bottom of the stack. */
938a0c9d52SAugustin Cavalier#define B_CLONEABLE_AREA		(1 << 8)
94c321cf49SAxel Dörfler
957af3465eSAxel Dörflerextern area_id		create_area(const char *name, void **startAddress,
967af3465eSAxel Dörfler						uint32 addressSpec, size_t size, uint32 lock,
977af3465eSAxel Dörfler						uint32 protection);
987af3465eSAxel Dörflerextern area_id		clone_area(const char *name, void **destAddress,
997af3465eSAxel Dörfler						uint32 addressSpec, uint32 protection, area_id source);
1007af3465eSAxel Dörflerextern area_id		find_area(const char *name);
1017af3465eSAxel Dörflerextern area_id		area_for(void *address);
1027af3465eSAxel Dörflerextern status_t		delete_area(area_id id);
1037af3465eSAxel Dörflerextern status_t		resize_area(area_id id, size_t newSize);
1047af3465eSAxel Dörflerextern status_t		set_area_protection(area_id id, uint32 newProtection);
105c321cf49SAxel Dörfler
106c321cf49SAxel Dörfler/* system private, use macros instead */
1077af3465eSAxel Dörflerextern status_t		_get_area_info(area_id id, area_info *areaInfo, size_t size);
1086e2f6d1aSAlex Smithextern status_t		_get_next_area_info(team_id team, ssize_t *cookie,
1097af3465eSAxel Dörfler						area_info *areaInfo, size_t size);
110c321cf49SAxel Dörfler
1111c016e0eSAxel Dörfler#define get_area_info(id, areaInfo) \
1127af3465eSAxel Dörfler	_get_area_info((id), (areaInfo),sizeof(*(areaInfo)))
1131c016e0eSAxel Dörfler#define get_next_area_info(team, cookie, areaInfo) \
1147af3465eSAxel Dörfler	_get_next_area_info((team), (cookie), (areaInfo), sizeof(*(areaInfo)))
11752a38012Sejakowatz/* Ports */
118c321cf49SAxel Dörfler
11952a38012Sejakowatztypedef struct port_info {
12052a38012Sejakowatz	port_id		port;
12152a38012Sejakowatz	team_id		team;
12252a38012Sejakowatz	char		name[B_OS_NAME_LENGTH];
123e1f3108aSStephan Aßmus	int32		capacity;		/* queue depth */
124e1f3108aSStephan Aßmus	int32		queue_count;	/* # msgs waiting to be read */
125e1f3108aSStephan Aßmus	int32		total_count;	/* total # msgs read so far */
12652a38012Sejakowatz} port_info;
1287af3465eSAxel Dörflerextern port_id		create_port(int32 capacity, const char *name);
1297af3465eSAxel Dörflerextern port_id		find_port(const char *name);
1307af3465eSAxel Dörflerextern ssize_t		read_port(port_id port, int32 *code, void *buffer,
1317af3465eSAxel Dörfler						size_t bufferSize);
1327af3465eSAxel Dörflerextern ssize_t		read_port_etc(port_id port, int32 *code, void *buffer,
1337af3465eSAxel Dörfler						size_t bufferSize, uint32 flags, bigtime_t timeout);
1347af3465eSAxel Dörflerextern status_t		write_port(port_id port, int32 code, const void *buffer,
1357af3465eSAxel Dörfler						size_t bufferSize);
1367af3465eSAxel Dörflerextern status_t		write_port_etc(port_id port, int32 code, const void *buffer,
1377af3465eSAxel Dörfler						size_t bufferSize, uint32 flags, bigtime_t timeout);
1387af3465eSAxel Dörflerextern status_t		close_port(port_id port);
1397af3465eSAxel Dörflerextern status_t		delete_port(port_id port);
1407af3465eSAxel Dörfler
1417af3465eSAxel Dörflerextern ssize_t		port_buffer_size(port_id port);
1427af3465eSAxel Dörflerextern ssize_t		port_buffer_size_etc(port_id port, uint32 flags,
1437af3465eSAxel Dörfler						bigtime_t timeout);
1447af3465eSAxel Dörflerextern ssize_t		port_count(port_id port);
1457af3465eSAxel Dörflerextern status_t		set_port_owner(port_id port, team_id team);
146c321cf49SAxel Dörfler
147c321cf49SAxel Dörfler/* system private, use the macros instead */
1487af3465eSAxel Dörflerextern status_t		_get_port_info(port_id port, port_info *portInfo,
1497af3465eSAxel Dörfler						size_t portInfoSize);
1507af3465eSAxel Dörflerextern status_t		_get_next_port_info(team_id team, int32 *cookie,
1517af3465eSAxel Dörfler						port_info *portInfo, size_t portInfoSize);
152c321cf49SAxel Dörfler
153c321cf49SAxel Dörfler#define get_port_info(port, info) \
1547af3465eSAxel Dörfler	_get_port_info((port), (info), sizeof(*(info)))
155c321cf49SAxel Dörfler#define get_next_port_info(team, cookie, info) \
1567af3465eSAxel Dörfler	_get_next_port_info((team), (cookie), (info), sizeof(*(info)))
157c321cf49SAxel Dörfler
158c321cf49SAxel Dörfler
159e1f3108aSStephan Aßmus/* WARNING: The following is Haiku experimental API. It might be removed or
160e1f3108aSStephan Aßmus   changed in the future. */
1617727e08eSIngo Weinhold
1627727e08eSIngo Weinholdtypedef struct port_message_info {
1637727e08eSIngo Weinhold	size_t		size;
1647727e08eSIngo Weinhold	uid_t		sender;
1657727e08eSIngo Weinhold	gid_t		sender_group;
1667727e08eSIngo Weinhold	team_id		sender_team;
1677727e08eSIngo Weinhold} port_message_info;
1687727e08eSIngo Weinhold
169e1f3108aSStephan Aßmus/* similar to port_buffer_size_etc(), but returns (more) info */
1707af3465eSAxel Dörflerextern status_t		_get_port_message_info_etc(port_id port,
1717af3465eSAxel Dörfler						port_message_info *info, size_t infoSize, uint32 flags,
1727af3465eSAxel Dörfler						bigtime_t timeout);
1737727e08eSIngo Weinhold
1747727e08eSIngo Weinhold#define get_port_message_info_etc(port, info, flags, timeout) \
1757af3465eSAxel Dörfler	_get_port_message_info_etc((port), (info), sizeof(*(info)), flags, timeout)
1767af3465eSAxel Dörfler
1777727e08eSIngo Weinhold
178c321cf49SAxel Dörfler/* Semaphores */
179c321cf49SAxel Dörfler
18052a38012Sejakowatztypedef struct sem_info {
18152a38012Sejakowatz	sem_id		sem;
1823cfbecf1Slillo	team_id		team;
18352a38012Sejakowatz	char		name[B_OS_NAME_LENGTH];
18452a38012Sejakowatz	int32		count;
18552a38012Sejakowatz	thread_id	latest_holder;
18652a38012Sejakowatz} sem_info;
188c321cf49SAxel Dörfler/* semaphore flags */
189c321cf49SAxel Dörflerenum {
190e1f3108aSStephan Aßmus	B_CAN_INTERRUPT				= 0x01,	/* acquisition of the semaphore can be
191e1f3108aSStephan Aßmus										   interrupted (system use only) */
192e1f3108aSStephan Aßmus	B_CHECK_PERMISSION			= 0x04,	/* ownership will be checked (system use
193e1f3108aSStephan Aßmus										   only) */
194e1f3108aSStephan Aßmus	B_KILL_CAN_INTERRUPT		= 0x20,	/* acquisition of the semaphore can be
195e1f3108aSStephan Aßmus										   interrupted by SIGKILL[THR], even
196e1f3108aSStephan Aßmus										   if not B_CAN_INTERRUPT (system use
197e1f3108aSStephan Aßmus										   only) */
1989de96900SAxel Dörfler
1999de96900SAxel Dörfler	/* release_sem_etc() only flags */
200e1f3108aSStephan Aßmus	B_DO_NOT_RESCHEDULE			= 0x02,	/* thread is not rescheduled */
201e1f3108aSStephan Aßmus	B_RELEASE_ALL				= 0x08,	/* all waiting threads will be woken up,
202e1f3108aSStephan Aßmus										   count will be zeroed */
203e1f3108aSStephan Aßmus	B_RELEASE_IF_WAITING_ONLY	= 0x10	/* release count only if there are any
204e1f3108aSStephan Aßmus										   threads waiting */
205c321cf49SAxel Dörfler};
2077af3465eSAxel Dörflerextern sem_id		create_sem(int32 count, const char *name);
2087af3465eSAxel Dörflerextern status_t		delete_sem(sem_id id);
2097af3465eSAxel Dörflerextern status_t		acquire_sem(sem_id id);
2107af3465eSAxel Dörflerextern status_t		acquire_sem_etc(sem_id id, int32 count, uint32 flags,
2117af3465eSAxel Dörfler						bigtime_t timeout);
2127af3465eSAxel Dörflerextern status_t		release_sem(sem_id id);
2137af3465eSAxel Dörflerextern status_t		release_sem_etc(sem_id id, int32 count, uint32 flags);
2147af3465eSAxel Dörfler/* TODO: the following two calls are not part of the BeOS API, and might be
215e1f3108aSStephan Aßmus   changed or even removed for the final release of Haiku R1 */
2167af3465eSAxel Dörflerextern status_t		switch_sem(sem_id semToBeReleased, sem_id id);
2177af3465eSAxel Dörflerextern status_t		switch_sem_etc(sem_id semToBeReleased, sem_id id,
2187af3465eSAxel Dörfler						int32 count, uint32 flags, bigtime_t timeout);
2197af3465eSAxel Dörflerextern status_t		get_sem_count(sem_id id, int32 *threadCount);
2207af3465eSAxel Dörflerextern status_t		set_sem_owner(sem_id id, team_id team);
222c321cf49SAxel Dörfler/* system private, use the macros instead */
2237af3465eSAxel Dörflerextern status_t		_get_sem_info(sem_id id, struct sem_info *info,
2247af3465eSAxel Dörfler						size_t infoSize);
2257af3465eSAxel Dörflerextern status_t		_get_next_sem_info(team_id team, int32 *cookie,
2267af3465eSAxel Dörfler						struct sem_info *info, size_t infoSize);
228c321cf49SAxel Dörfler#define get_sem_info(sem, info) \
2297af3465eSAxel Dörfler	_get_sem_info((sem), (info), sizeof(*(info)))
231c321cf49SAxel Dörfler#define get_next_sem_info(team, cookie, info) \
2327af3465eSAxel Dörfler	_get_next_sem_info((team), (cookie), (info), sizeof(*(info)))
235c321cf49SAxel Dörfler/* Teams */
237c321cf49SAxel Dörflertypedef struct {
238c321cf49SAxel Dörfler	team_id			team;
239c321cf49SAxel Dörfler	int32			thread_count;
240c321cf49SAxel Dörfler	int32			image_count;
241c321cf49SAxel Dörfler	int32			area_count;
242c321cf49SAxel Dörfler	thread_id		debugger_nub_thread;
243c321cf49SAxel Dörfler	port_id			debugger_nub_port;
244c321cf49SAxel Dörfler	int32			argc;
245c321cf49SAxel Dörfler	char			args[64];
246c321cf49SAxel Dörfler	uid_t			uid;
247c321cf49SAxel Dörfler	gid_t			gid;
248c321cf49SAxel Dörfler} team_info;
2507ba272b8SAxel Dörfler#define B_CURRENT_TEAM	0
251b2c5594fSAxel Dörfler#define B_SYSTEM_TEAM	1
252c321cf49SAxel Dörfler
2537af3465eSAxel Dörflerextern status_t		kill_team(team_id team);
254c321cf49SAxel Dörfler	/* see also: send_signal() */
255c321cf49SAxel Dörfler
256c321cf49SAxel Dörfler/* system private, use macros instead */
2577af3465eSAxel Dörflerextern status_t		_get_team_info(team_id id, team_info *info, size_t size);
2587af3465eSAxel Dörflerextern status_t		_get_next_team_info(int32 *cookie, team_info *info,
2597af3465eSAxel Dörfler						size_t size);
260c321cf49SAxel Dörfler
261c321cf49SAxel Dörfler#define get_team_info(id, info) \
2627af3465eSAxel Dörfler	_get_team_info((id), (info), sizeof(*(info)))
263c321cf49SAxel Dörfler
264c321cf49SAxel Dörfler#define get_next_team_info(cookie, info) \
2657af3465eSAxel Dörfler	_get_next_team_info((cookie), (info), sizeof(*(info)))
266c321cf49SAxel Dörfler
267c321cf49SAxel Dörfler/* team usage info */
268c321cf49SAxel Dörfler
269c321cf49SAxel Dörflertypedef struct {
270c321cf49SAxel Dörfler	bigtime_t		user_time;
271c321cf49SAxel Dörfler	bigtime_t		kernel_time;
272c321cf49SAxel Dörfler} team_usage_info;
273c321cf49SAxel Dörfler
27440bfa3f7SAxel Dörflerenum {
275d76bee07SAxel Dörfler	/* compatible to sys/resource.h RUSAGE_SELF and RUSAGE_CHILDREN */
27640bfa3f7SAxel Dörfler	B_TEAM_USAGE_SELF		= 0,
2772c97a8d1SIngo Weinhold	B_TEAM_USAGE_CHILDREN	= -1
27840bfa3f7SAxel Dörfler};
27940bfa3f7SAxel Dörfler
280c321cf49SAxel Dörfler/* system private, use macros instead */
2817af3465eSAxel Dörflerextern status_t		_get_team_usage_info(team_id team, int32 who,
2827af3465eSAxel Dörfler						team_usage_info *info, size_t size);
284c321cf49SAxel Dörfler#define get_team_usage_info(team, who, info) \
2857af3465eSAxel Dörfler	_get_team_usage_info((team), (who), (info), sizeof(*(info)))
2867af3465eSAxel Dörfler
288c321cf49SAxel Dörfler/* Threads */
290c321cf49SAxel Dörflertypedef enum {
291c321cf49SAxel Dörfler	B_THREAD_RUNNING	= 1,
292c321cf49SAxel Dörfler	B_THREAD_READY,
293c321cf49SAxel Dörfler	B_THREAD_RECEIVING,
294c321cf49SAxel Dörfler	B_THREAD_ASLEEP,
295c321cf49SAxel Dörfler	B_THREAD_SUSPENDED,
296c321cf49SAxel Dörfler	B_THREAD_WAITING
297c321cf49SAxel Dörfler} thread_state;
2993cfbecf1Slillotypedef struct {
30052a38012Sejakowatz	thread_id		thread;
30152a38012Sejakowatz	team_id			team;
30252a38012Sejakowatz	char			name[B_OS_NAME_LENGTH];
30352a38012Sejakowatz	thread_state	state;
30452a38012Sejakowatz	int32			priority;
30552a38012Sejakowatz	sem_id			sem;
30652a38012Sejakowatz	bigtime_t		user_time;
30752a38012Sejakowatz	bigtime_t		kernel_time;
30852a38012Sejakowatz	void			*stack_base;
30952a38012Sejakowatz	void			*stack_end;
31052a38012Sejakowatz} thread_info;
312c321cf49SAxel Dörfler#define B_IDLE_PRIORITY					0
313c321cf49SAxel Dörfler#define B_LOWEST_ACTIVE_PRIORITY		1
314c321cf49SAxel Dörfler#define B_LOW_PRIORITY					5
315c321cf49SAxel Dörfler#define B_NORMAL_PRIORITY				10
316c321cf49SAxel Dörfler#define B_DISPLAY_PRIORITY				15
317c321cf49SAxel Dörfler#define	B_URGENT_DISPLAY_PRIORITY		20
318c321cf49SAxel Dörfler#define	B_REAL_TIME_DISPLAY_PRIORITY	100
319c321cf49SAxel Dörfler#define	B_URGENT_PRIORITY				110
320c321cf49SAxel Dörfler#define B_REAL_TIME_PRIORITY			120
322d7e489f8Sbeveloper#define B_SYSTEM_TIMEBASE				0
32324df6592SIngo Weinhold	/* time base for snooze_*(), compatible with the clockid_t constants defined
32424df6592SIngo Weinhold	   in <time.h> */
3272c1e463cSAxel Dörfler
3281c016e0eSAxel Dörflertypedef status_t (*thread_func)(void *);
3291c016e0eSAxel Dörfler#define thread_entry thread_func
3301c016e0eSAxel Dörfler	/* thread_entry is for backward compatibility only! Use thread_func */
3327af3465eSAxel Dörflerextern thread_id	spawn_thread(thread_func, const char *name, int32 priority,
3337af3465eSAxel Dörfler						void *data);
334c321cf49SAxel Dörflerextern status_t		kill_thread(thread_id thread);
335c321cf49SAxel Dörflerextern status_t		resume_thread(thread_id thread);
336c321cf49SAxel Dörflerextern status_t		suspend_thread(thread_id thread);
338c321cf49SAxel Dörflerextern status_t		rename_thread(thread_id thread, const char *newName);
339e628f41cSAxel Dörflerextern status_t		set_thread_priority(thread_id thread, int32 newPriority);
340c321cf49SAxel Dörflerextern void			exit_thread(status_t status);
341e628f41cSAxel Dörflerextern status_t		wait_for_thread(thread_id thread, status_t *returnValue);
342c321cf49SAxel Dörflerextern status_t		on_exit_thread(void (*callback)(void *), void *data);
344c321cf49SAxel Dörflerextern thread_id 	find_thread(const char *name);
346183dee22SAxel Dörflerextern status_t		send_data(thread_id thread, int32 code, const void *buffer,
347183dee22SAxel Dörfler						size_t bufferSize);
3487af3465eSAxel Dörflerextern int32		receive_data(thread_id *sender, void *buffer,
3497af3465eSAxel Dörfler						size_t bufferSize);
350c321cf49SAxel Dörflerextern bool			has_data(thread_id thread);
3513088c8d0SDavid Reid
352c321cf49SAxel Dörflerextern status_t		snooze(bigtime_t amount);
3533f403c72SAxel Dörflerextern status_t		snooze_etc(bigtime_t amount, int timeBase, uint32 flags);
354c321cf49SAxel Dörflerextern status_t		snooze_until(bigtime_t time, int timeBase);
356c321cf49SAxel Dörfler/* system private, use macros instead */
357c321cf49SAxel Dörflerextern status_t		_get_thread_info(thread_id id, thread_info *info, size_t size);
358183dee22SAxel Dörflerextern status_t		_get_next_thread_info(team_id team, int32 *cookie,
359183dee22SAxel Dörfler						thread_info *info, size_t size);
360c321cf49SAxel Dörfler
361c321cf49SAxel Dörfler#define get_thread_info(id, info) \
3627af3465eSAxel Dörfler	_get_thread_info((id), (info), sizeof(*(info)))
364c321cf49SAxel Dörfler#define get_next_thread_info(team, cookie, info) \
3657af3465eSAxel Dörfler	_get_next_thread_info((team), (cookie), (info), sizeof(*(info)))
3679c7b2520SIngo Weinhold/* bridge to the pthread API */
3689c7b2520SIngo Weinholdextern thread_id	get_pthread_thread_id(pthread_t thread);
3699c7b2520SIngo Weinhold/* TODO: Would be nice to have, but we use TLS to associate a thread with its
3709c7b2520SIngo Weinhold   pthread object. So this is not trivial to implement.
3719c7b2520SIngo Weinholdextern status_t		convert_to_pthread(thread_id thread, pthread_t *_thread);
3729c7b2520SIngo Weinhold*/
3739c7b2520SIngo Weinhold
375c321cf49SAxel Dörfler/* Time */
376c321cf49SAxel Dörfler
3777d8eb4d7SAugustin Cavalierextern unsigned long real_time_clock(void);
3787d8eb4d7SAugustin Cavalierextern void			set_real_time_clock(unsigned long secsSinceJan1st1970);
379c321cf49SAxel Dörflerextern bigtime_t	real_time_clock_usecs(void);
3807af3465eSAxel Dörflerextern bigtime_t	system_time(void);
3817af3465eSAxel Dörfler						/* time since booting in microseconds */
382586d22c9SFrançois Revolextern nanotime_t	system_time_nsecs(void);
38334a48c70SIngo Weinhold						/* time since booting in nanoseconds */
384c321cf49SAxel Dörfler
38597dfeb96SAdrien Destugues					/* deprecated (is no-op) */
386308ca58dSOliver Tappeextern status_t		set_timezone(const char *timezone);
387308ca58dSOliver Tappe
388c321cf49SAxel Dörfler/* Alarm */
389c321cf49SAxel Dörfler
390c321cf49SAxel Dörflerenum {
391c321cf49SAxel Dörfler	B_ONE_SHOT_ABSOLUTE_ALARM	= 1,
392c321cf49SAxel Dörfler	B_ONE_SHOT_RELATIVE_ALARM,
393c321cf49SAxel Dörfler	B_PERIODIC_ALARM			/* "when" specifies the period */
394c321cf49SAxel Dörfler};
395c321cf49SAxel Dörfler
3967af3465eSAxel Dörflerextern bigtime_t	set_alarm(bigtime_t when, uint32 flags);
397c321cf49SAxel Dörfler
398c321cf49SAxel Dörfler
399c321cf49SAxel Dörfler/* Debugger */
400c321cf49SAxel Dörfler
4017af3465eSAxel Dörflerextern void			debugger(const char *message);
402c321cf49SAxel Dörfler
403c321cf49SAxel Dörfler/*
404c321cf49SAxel Dörfler   calling this function with a non-zero value will cause your thread
405c321cf49SAxel Dörfler   to receive signals for any exceptional conditions that occur (i.e.
406c321cf49SAxel Dörfler   you'll get SIGSEGV for data access exceptions, SIGFPE for floating
407c321cf49SAxel Dörfler   point errors, SIGILL for illegal instructions, etc).
408c321cf49SAxel Dörfler
409c321cf49SAxel Dörfler   to re-enable the default debugger pass a zero.
4102c1e463cSAxel Dörfler*/
4117af3465eSAxel Dörflerextern int			disable_debugger(int state);
412c321cf49SAxel Dörfler
413e1f3108aSStephan Aßmus/* TODO: Remove. Temporary debug helper. */
4147af3465eSAxel Dörflerextern void			debug_printf(const char *format, ...)
4157af3465eSAxel Dörfler						__attribute__ ((format (__printf__, 1, 2)));
4167af3465eSAxel Dörflerextern void			debug_vprintf(const char *format, va_list args);
4177af3465eSAxel Dörflerextern void			ktrace_printf(const char *format, ...)
4187af3465eSAxel Dörfler						__attribute__ ((format (__printf__, 1, 2)));
4197af3465eSAxel Dörflerextern void			ktrace_vprintf(const char *format, va_list args);
42057b6c3bcSIngo Weinhold
421c321cf49SAxel Dörfler
422c321cf49SAxel Dörfler/* System information */
423c321cf49SAxel Dörfler
4241bc7045fSPawel Dziepaktypedef struct {
4251bc7045fSPawel Dziepak	bigtime_t	active_time;	/* usec of doing useful work since boot */
4261bc7045fSPawel Dziepak	bool		enabled;
4271bc7045fSPawel Dziepak} cpu_info;
4281bc7045fSPawel Dziepak
4291bc7045fSPawel Dziepaktypedef struct {
4301bc7045fSPawel Dziepak	bigtime_t		boot_time;			/* time of boot (usecs since 1/1/1970) */
4311bc7045fSPawel Dziepak
4321bc7045fSPawel Dziepak	uint32			cpu_count;			/* number of cpus */
4331bc7045fSPawel Dziepak
4341bc7045fSPawel Dziepak	uint64			max_pages;			/* total # of accessible pages */
4351bc7045fSPawel Dziepak	uint64			used_pages;			/* # of accessible pages in use */
4361bc7045fSPawel Dziepak	uint64			cached_pages;
4371bc7045fSPawel Dziepak	uint64			block_cache_pages;
4381bc7045fSPawel Dziepak	uint64			ignored_pages;		/* # of ignored/inaccessible pages */
4391bc7045fSPawel Dziepak
440d02aaee1SPawel Dziepak	uint64			needed_memory;
441d02aaee1SPawel Dziepak	uint64			free_memory;
442d02aaee1SPawel Dziepak
4431bc7045fSPawel Dziepak	uint64			max_swap_pages;
444d02aaee1SPawel Dziepak	uint64			free_swap_pages;
4451bc7045fSPawel Dziepak
4461bc7045fSPawel Dziepak	uint32			page_faults;		/* # of page faults */
4471bc7045fSPawel Dziepak
4481bc7045fSPawel Dziepak	uint32			max_sems;
4491bc7045fSPawel Dziepak	uint32			used_sems;
4501bc7045fSPawel Dziepak
4511bc7045fSPawel Dziepak	uint32			max_ports;
4521bc7045fSPawel Dziepak	uint32			used_ports;
4531bc7045fSPawel Dziepak
4541bc7045fSPawel Dziepak	uint32			max_threads;
4551bc7045fSPawel Dziepak	uint32			used_threads;
4561bc7045fSPawel Dziepak
4571bc7045fSPawel Dziepak	uint32			max_teams;
4581bc7045fSPawel Dziepak	uint32			used_teams;
459c321cf49SAxel Dörfler
4601bc7045fSPawel Dziepak	char			kernel_name[B_FILE_NAME_LENGTH];
4611bc7045fSPawel Dziepak	char			kernel_build_date[B_OS_NAME_LENGTH];
4621bc7045fSPawel Dziepak	char			kernel_build_time[B_OS_NAME_LENGTH];
4631bc7045fSPawel Dziepak
4641bc7045fSPawel Dziepak	int64			kernel_version;
4651bc7045fSPawel Dziepak	uint32			abi;				/* the system API */
4661bc7045fSPawel Dziepak} system_info;
4671bc7045fSPawel Dziepak
4681bc7045fSPawel Dziepakenum topology_level_type {
4691bc7045fSPawel Dziepak	B_TOPOLOGY_UNKNOWN,
4701bc7045fSPawel Dziepak	B_TOPOLOGY_ROOT,
4711bc7045fSPawel Dziepak	B_TOPOLOGY_SMT,
4721bc7045fSPawel Dziepak	B_TOPOLOGY_CORE,
4731bc7045fSPawel Dziepak	B_TOPOLOGY_PACKAGE
4741bc7045fSPawel Dziepak};
4751bc7045fSPawel Dziepak
4761bc7045fSPawel Dziepakenum cpu_platform {
4771bc7045fSPawel Dziepak	B_CPU_UNKNOWN,
478bc93cc3fSAxel Dörfler	B_CPU_x86,
47914919567SAlexander von Gluck IV	B_CPU_x86_64,
48014919567SAlexander von Gluck IV	B_CPU_PPC,
48114919567SAlexander von Gluck IV	B_CPU_PPC_64,
48214919567SAlexander von Gluck IV	B_CPU_M68K,
48314919567SAlexander von Gluck IV	B_CPU_ARM,
48414919567SAlexander von Gluck IV	B_CPU_ARM_64,
48514919567SAlexander von Gluck IV	B_CPU_ALPHA,
48614919567SAlexander von Gluck IV	B_CPU_MIPS,
48714919567SAlexander von Gluck IV	B_CPU_SH
4881bc7045fSPawel Dziepak};
4891bc7045fSPawel Dziepak
4901bc7045fSPawel Dziepakenum cpu_vendor {
4911bc7045fSPawel Dziepak	B_CPU_VENDOR_UNKNOWN,
4921bc7045fSPawel Dziepak	B_CPU_VENDOR_AMD,
4931bc7045fSPawel Dziepak	B_CPU_VENDOR_CYRIX,
4941bc7045fSPawel Dziepak	B_CPU_VENDOR_IDT,
4951bc7045fSPawel Dziepak	B_CPU_VENDOR_INTEL,
4971bc7045fSPawel Dziepak	B_CPU_VENDOR_RISE,
4981bc7045fSPawel Dziepak	B_CPU_VENDOR_TRANSMETA,
49914919567SAlexander von Gluck IV	B_CPU_VENDOR_VIA,
50014919567SAlexander von Gluck IV	B_CPU_VENDOR_IBM,
50114919567SAlexander von Gluck IV	B_CPU_VENDOR_MOTOROLA,
50214919567SAlexander von Gluck IV	B_CPU_VENDOR_NEC
5031bc7045fSPawel Dziepak};
5041bc7045fSPawel Dziepak
5051bc7045fSPawel Dziepaktypedef struct {
5061bc7045fSPawel Dziepak	enum cpu_platform		platform;
5071bc7045fSPawel Dziepak} cpu_topology_root_info;
5081bc7045fSPawel Dziepak
5091bc7045fSPawel Dziepaktypedef struct {
5101bc7045fSPawel Dziepak	enum cpu_vendor			vendor;
5111bc7045fSPawel Dziepak	uint32					cache_line_size;
5121bc7045fSPawel Dziepak} cpu_topology_package_info;
5131bc7045fSPawel Dziepak
5141bc7045fSPawel Dziepaktypedef struct {
5151bc7045fSPawel Dziepak	uint32					model;
5161bc7045fSPawel Dziepak	uint64					default_frequency;
5171bc7045fSPawel Dziepak} cpu_topology_core_info;
5181bc7045fSPawel Dziepak
5191bc7045fSPawel Dziepaktypedef struct {
5201bc7045fSPawel Dziepak	uint32							id;
5211bc7045fSPawel Dziepak	enum topology_level_type		type;
5221bc7045fSPawel Dziepak	uint32							level;
5231bc7045fSPawel Dziepak
5241bc7045fSPawel Dziepak	union {
5251bc7045fSPawel Dziepak		cpu_topology_root_info		root;
5261bc7045fSPawel Dziepak		cpu_topology_package_info	package;
5271bc7045fSPawel Dziepak		cpu_topology_core_info		core;
5281bc7045fSPawel Dziepak	} data;
5291bc7045fSPawel Dziepak} cpu_topology_node_info;
5301bc7045fSPawel Dziepak
5311bc7045fSPawel Dziepak
5321bc7045fSPawel Dziepakextern status_t		get_system_info(system_info* info);
5331bc7045fSPawel Dziepakextern status_t		get_cpu_info(uint32 firstCPU, uint32 cpuCount,
5341bc7045fSPawel Dziepak						cpu_info* info);
5351bc7045fSPawel Dziepakextern status_t		get_cpu_topology_info(cpu_topology_node_info* topologyInfos,
5361bc7045fSPawel Dziepak						uint32* topologyInfoCount);
537c321cf49SAxel Dörfler
5385ffbe7d7SAugustin Cavalier#if defined(__i386__) || defined(__x86_64__)
539c321cf49SAxel Dörflertypedef union {
540c321cf49SAxel Dörfler	struct {
541c321cf49SAxel Dörfler		uint32	max_eax;
542bc93cc3fSAxel Dörfler		char	vendor_id[12];
543c321cf49SAxel Dörfler	} eax_0;
544c321cf49SAxel Dörfler
545c321cf49SAxel Dörfler	struct {
546bc93cc3fSAxel Dörfler		uint32	stepping		: 4;
547bc93cc3fSAxel Dörfler		uint32	model			: 4;
548bc93cc3fSAxel Dörfler		uint32	family			: 4;
549bc93cc3fSAxel Dörfler		uint32	type			: 2;
550bc93cc3fSAxel Dörfler		uint32	reserved_0		: 2;
551bc93cc3fSAxel Dörfler		uint32	extended_model	: 4;
552bc93cc3fSAxel Dörfler		uint32	extended_family	: 8;
553bc93cc3fSAxel Dörfler		uint32	reserved_1		: 4;
554bc93cc3fSAxel Dörfler
5551c016e0eSAxel Dörfler		uint32	brand_index		: 8;
5561c016e0eSAxel Dörfler		uint32	clflush			: 8;
5571c016e0eSAxel Dörfler		uint32	logical_cpus	: 8;
5581c016e0eSAxel Dörfler		uint32	apic_id			: 8;
5591c016e0eSAxel Dörfler
5601c016e0eSAxel Dörfler		uint32	features;
5619235adf7SAxel Dörfler		uint32	extended_features;
562c321cf49SAxel Dörfler	} eax_1;
563c321cf49SAxel Dörfler
56406e37d1eSAxel Dörfler	struct {
565c321cf49SAxel Dörfler		uint8	call_num;
566c321cf49SAxel Dörfler		uint8	cache_descriptors[15];
567c321cf49SAxel Dörfler	} eax_2;
568c321cf49SAxel Dörfler
569c321cf49SAxel Dörfler	struct {
570c321cf49SAxel Dörfler		uint32	reserved[2];
571c321cf49SAxel Dörfler		uint32	serial_number_high;
572c321cf49SAxel Dörfler		uint32	serial_number_low;
573c321cf49SAxel Dörfler	} eax_3;
574c321cf49SAxel Dörfler
575c321cf49SAxel Dörfler	char		as_chars[16];
576c321cf49SAxel Dörfler
577c321cf49SAxel Dörfler	struct {
578c321cf49SAxel Dörfler		uint32	eax;
579c321cf49SAxel Dörfler		uint32	ebx;
580c321cf49SAxel Dörfler		uint32	edx;
581c321cf49SAxel Dörfler		uint32	ecx;
5822c1e463cSAxel Dörfler	} regs;
583c321cf49SAxel Dörfler} cpuid_info;
584c321cf49SAxel Dörfler
5857af3465eSAxel Dörflerextern status_t		get_cpuid(cpuid_info *info, uint32 eaxRegister,
5867af3465eSAxel Dörfler						uint32 cpuNum);
587c321cf49SAxel Dörfler#endif
588c321cf49SAxel Dörfler
589c321cf49SAxel Dörfler
5907af3465eSAxel Dörflerextern int32		is_computer_on(void);
5917af3465eSAxel Dörflerextern double		is_computer_on_fire(void);
593636bfc08SIngo Weinhold
59424df6592SIngo Weinhold/* signal related functions */
59524df6592SIngo Weinholdint		send_signal(thread_id threadID, unsigned int signal);
59624df6592SIngo Weinholdvoid	set_signal_stack(void* base, size_t size);
59724df6592SIngo Weinhold
59824df6592SIngo Weinhold
599e1f3108aSStephan Aßmus/* WARNING: Experimental API! */
600636bfc08SIngo Weinhold
601636bfc08SIngo Weinholdenum {
602636bfc08SIngo Weinhold	B_OBJECT_TYPE_FD		= 0,
603636bfc08SIngo Weinhold	B_OBJECT_TYPE_SEMAPHORE	= 1,
604636bfc08SIngo Weinhold	B_OBJECT_TYPE_PORT		= 2,
605636bfc08SIngo Weinhold	B_OBJECT_TYPE_THREAD	= 3
606636bfc08SIngo Weinhold};
607636bfc08SIngo Weinhold
608636bfc08SIngo Weinholdenum {
609e1f3108aSStephan Aßmus	B_EVENT_READ				= 0x0001,	/* FD/port readable */
610e1f3108aSStephan Aßmus	B_EVENT_WRITE				= 0x0002,	/* FD/port writable */
611e1f3108aSStephan Aßmus	B_EVENT_ERROR				= 0x0004,	/* FD error */
612e1f3108aSStephan Aßmus	B_EVENT_PRIORITY_READ		= 0x0008,	/* FD priority readable */
613e1f3108aSStephan Aßmus	B_EVENT_PRIORITY_WRITE		= 0x0010,	/* FD priority writable */
614e1f3108aSStephan Aßmus	B_EVENT_HIGH_PRIORITY_READ	= 0x0020,	/* FD high priority readable */
615e1f3108aSStephan Aßmus	B_EVENT_HIGH_PRIORITY_WRITE	= 0x0040,	/* FD high priority writable */
616e1f3108aSStephan Aßmus	B_EVENT_DISCONNECTED		= 0x0080,	/* FD disconnected */
617e1f3108aSStephan Aßmus
618e1f3108aSStephan Aßmus	B_EVENT_ACQUIRE_SEMAPHORE	= 0x0001,	/* semaphore can be acquired */
619e1f3108aSStephan Aßmus
620e1f3108aSStephan Aßmus	B_EVENT_INVALID				= 0x1000	/* FD/port/sem/thread ID not or
621e1f3108aSStephan Aßmus											   no longer valid (e.g. has been
622e1f3108aSStephan Aßmus											   close/deleted) */
623636bfc08SIngo Weinhold};
624636bfc08SIngo Weinhold
625636bfc08SIngo Weinholdtypedef struct object_wait_info {
626e1f3108aSStephan Aßmus	int32		object;						/* ID of the object */
627e1f3108aSStephan Aßmus	uint16		type;						/* type of the object */
628e1f3108aSStephan Aßmus	uint16		events;						/* events mask */
629636bfc08SIngo Weinhold} object_wait_info;
630636bfc08SIngo Weinhold
631e1f3108aSStephan Aßmus/* wait_for_objects[_etc]() waits until at least one of the specified events or,
632e1f3108aSStephan Aßmus   if given, the timeout occurred. When entering the function the
633e1f3108aSStephan Aßmus   object_wait_info::events field specifies the events for each object the
634e1f3108aSStephan Aßmus   caller is interested in. When the function returns the fields reflect the
635e1f3108aSStephan Aßmus   events that actually occurred. The events B_EVENT_INVALID, B_EVENT_ERROR,
636e1f3108aSStephan Aßmus   and B_EVENT_DISCONNECTED don't need to be specified. They will always be
637e1f3108aSStephan Aßmus   reported, when they occur. */
638636bfc08SIngo Weinhold
6397af3465eSAxel Dörflerextern ssize_t		wait_for_objects(object_wait_info* infos, int numInfos);
6407af3465eSAxel Dörflerextern ssize_t		wait_for_objects_etc(object_wait_info* infos, int numInfos,
6417af3465eSAxel Dörfler						uint32 flags, bigtime_t timeout);
642636bfc08SIngo Weinhold
643636bfc08SIngo Weinhold
64452a38012Sejakowatz#ifdef __cplusplus
64852a38012Sejakowatz#endif /* _OS_H */