xref: /haiku/headers/os/kernel/OS.h (revision d7e489f80a82a0dc5974df1e780d7a908129bab4)
1 /* OS.h - kernel specific structures and functions
2 **
3 ** Distributed under the terms of the OpenBeOS License.
4 */
5 
6 #ifndef _OS_H
7 #define _OS_H
8 
9 //#include <ktypes.h>
10 // ToDo: has to be included here, but does currently causes compiler errors
11 // (redefinitions in ktypes.h (bigtime_t), and arch/x86/types.h (int8, ...))
12 #include <SupportDefs.h>
13 #include <StorageDefs.h>
14 // ToDo: is automatically included by SupportDefs.h, so that one can be removed later
15 //#include <Errors.h>
16 
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 /*-------------------------------------------------------------*/
23 /* System constants */
24 
25 #define B_OS_NAME_LENGTH	32
26 #define B_PAGE_SIZE			4096
27 #define B_INFINITE_TIMEOUT	(9223372036854775807LL)
28 
29 enum {
30 	B_TIMEOUT			= 8,	/* relative timeout */
31 	B_RELATIVE_TIMEOUT	= 8,	/* fails after a relative timeout with B_WOULD_BLOCK */
32 	B_ABSOLUTE_TIMEOUT	= 16,	/* fails after an absolute timeout with B_WOULD BLOCK */
33 };
34 
35 /*-------------------------------------------------------------*/
36 /* Types */
37 
38 typedef int32 area_id;
39 typedef int32 port_id;
40 typedef int32 sem_id;
41 typedef int32 team_id;
42 typedef int32 thread_id;
43 
44 
45 /*-------------------------------------------------------------*/
46 /* Areas */
47 
48 typedef struct area_info {
49 	area_id		area;
50 	char		name[B_OS_NAME_LENGTH];
51 	size_t		size;
52 	uint32		lock;
53 	uint32		protection;
54 	team_id		team;
55 	uint32		ram_size;
56 	uint32		copy_count;
57 	uint32		in_count;
58 	uint32		out_count;
59 	void		*address;
60 } area_info;
61 
62 /* area locking */
63 #define B_NO_LOCK				0
64 #define B_LAZY_LOCK				1
65 #define B_FULL_LOCK				2
66 #define B_CONTIGUOUS			3
67 #define	B_LOMEM					4
68 
69 /* address spec for create_area(), and clone_area() */
70 #define B_ANY_ADDRESS			0
71 #define B_EXACT_ADDRESS			1
72 #define B_BASE_ADDRESS			2
73 #define B_CLONE_ADDRESS			3
74 #define	B_ANY_KERNEL_ADDRESS	4
75 
76 /* area protection */
77 #define B_READ_AREA				1
78 #define B_WRITE_AREA			2
79 
80 extern area_id	create_area(const char *name, void **start_addr, uint32 addr_spec,
81 					size_t size, uint32 lock, uint32 protection);
82 extern area_id	clone_area(const char *name, void **dest_addr, uint32 addr_spec,
83 					uint32 protection, area_id source);
84 extern area_id	find_area(const char *name);
85 extern area_id	area_for(void *address);
86 extern status_t	delete_area(area_id id);
87 extern status_t	resize_area(area_id id, size_t new_size);
88 extern status_t	set_area_protection(area_id id, uint32 new_protection);
89 
90 /* system private, use macros instead */
91 extern status_t	_get_area_info(area_id id, area_info *areaInfo, size_t size);
92 extern status_t	_get_next_area_info(team_id team, int32 *cookie, area_info *areaInfo, size_t size);
93 
94 #define get_area_info(id, ainfo) \
95 			_get_area_info((id), (ainfo),sizeof(*(ainfo)))
96 #define get_next_area_info(team, cookie, ainfo) \
97 			_get_next_area_info((team), (cookie), (ainfo), sizeof(*(ainfo)))
98 
99 
100 /*-------------------------------------------------------------*/
101 /* Ports */
102 
103 typedef struct port_info {
104 	port_id		port;
105 	team_id		team;
106 	char		name[B_OS_NAME_LENGTH];
107 	int32		capacity; /* queue depth */
108 	int32		queue_count; /* # msgs waiting to be read */
109 	int32		total_count; /* total # msgs read so far */
110 } port_info;
111 
112 extern port_id	create_port(int32 capacity, const char *name);
113 extern port_id	find_port(const char *name);
114 extern status_t read_port(port_id port, int32 *code, void *buffer, size_t bufferSize);
115 extern status_t read_port_etc(port_id port, int32 *code, void *buffer, size_t bufferSize,
116 					uint32 flags, bigtime_t timeout);
117 extern status_t write_port(port_id port, int32 code, const void *buffer, size_t bufferSize);
118 extern status_t write_port_etc(port_id port, int32 code, const void *buffer, size_t bufferSize,
119 					uint32 flags, bigtime_t timeout);
120 extern status_t close_port(port_id port);
121 extern status_t delete_port(port_id port);
122 
123 extern ssize_t	port_buffer_size(port_id port);
124 extern ssize_t	port_buffer_size_etc(port_id port, uint32 flags, bigtime_t timeout);
125 extern ssize_t	port_count(port_id port);
126 extern status_t set_port_owner(port_id port, team_id team);
127 
128 /* system private, use the macros instead */
129 extern status_t _get_port_info(port_id port, port_info *portInfo, size_t portInfoSize);
130 extern status_t _get_next_port_info(team_id team, int32 *cookie, port_info *portInfo,
131 					size_t portInfoSize);
132 
133 #define get_port_info(port, info) \
134 			_get_port_info((port), (info), sizeof(*(info)))
135 #define get_next_port_info(team, cookie, info) \
136 			_get_next_port_info((team), (cookie), (info), sizeof(*(info)))
137 
138 
139 /*-------------------------------------------------------------*/
140 /* Semaphores */
141 
142 typedef struct sem_info {
143 	sem_id		sem;
144 	team_id		team;
145 	char		name[B_OS_NAME_LENGTH];
146 	int32		count;
147 	thread_id	latest_holder;
148 } sem_info;
149 
150 /* semaphore flags */
151 enum {
152 	B_CAN_INTERRUPT		= 1,	/* acquisition of the semaphore can be interrupted (system use only) */
153 	B_DO_NOT_RESCHEDULE	= 2,	/* thread is not rescheduled after release_sem_etc() */
154 	B_CHECK_PERMISSION	= 4,	/* ownership will be checked (system use only) */
155 };
156 
157 extern sem_id	create_sem(int32 count, const char *name);
158 extern status_t	delete_sem(sem_id id);
159 extern status_t	delete_sem_etc(sem_id id, status_t returnCode); /* ToDo: not public BeOS */
160 extern status_t	acquire_sem(sem_id id);
161 extern status_t	acquire_sem_etc(sem_id id, int32 count, uint32 flags, bigtime_t timeout);
162 extern status_t	release_sem(sem_id id);
163 extern status_t	release_sem_etc(sem_id id, int32 count, uint32 flags);
164 extern status_t	get_sem_count(sem_id id, int32 *threadCount);
165 extern status_t	set_sem_owner(sem_id id, team_id team);
166 
167 /* system private, use the macros instead */
168 extern status_t	_get_sem_info(sem_id id, struct sem_info *info, size_t infoSize);
169 extern status_t	_get_next_sem_info(team_id team, int32 *cookie, struct sem_info *info,
170 					size_t infoSize);
171 
172 #define get_sem_info(sem, info) \
173 			_get_sem_info((sem), (info), sizeof(*(info)))
174 
175 #define get_next_sem_info(team, cookie, info) \
176 			_get_next_sem_info((team), (cookie), (info), sizeof(*(info)))
177 
178 
179 /*-------------------------------------------------------------*/
180 /* Teams */
181 
182 typedef struct {
183 	team_id			team;
184 	int32			thread_count;
185 	int32			image_count;
186 	int32			area_count;
187 	thread_id		debugger_nub_thread;
188 	port_id			debugger_nub_port;
189 	int32			argc;
190 	char			args[64];
191 	uid_t			uid;
192 	gid_t			gid;
193 } team_info;
194 
195 #define	B_SYSTEM_TEAM	2
196 
197 extern status_t kill_team(team_id team);
198 	/* see also: send_signal() */
199 
200 /* system private, use macros instead */
201 extern status_t _get_team_info(team_id id, team_info *info, size_t size);
202 extern status_t _get_next_team_info(int32 *cookie, team_info *info, size_t size);
203 
204 #define get_team_info(id, info) \
205 			_get_team_info((id), (info), sizeof(*(info)))
206 
207 #define get_next_team_info(cookie, info) \
208 			_get_next_team_info((cookie), (info), sizeof(*(info)))
209 
210 /* team usage info */
211 
212 typedef struct {
213 	bigtime_t		user_time;
214 	bigtime_t		kernel_time;
215 } team_usage_info;
216 
217 /* system private, use macros instead */
218 extern status_t	_get_team_usage_info(team_id team, int32 who, team_usage_info *tui, size_t size);
219 
220 #define get_team_usage_info(team, who, info) \
221 			_get_team_usage_info((team), (who), (info), sizeof(*(info)))
222 
223 /*-------------------------------------------------------------*/
224 /* Threads */
225 
226 typedef enum {
227 	B_THREAD_RUNNING	= 1,
228 	B_THREAD_READY,
229 	B_THREAD_RECEIVING,
230 	B_THREAD_ASLEEP,
231 	B_THREAD_SUSPENDED,
232 	B_THREAD_WAITING
233 } thread_state;
234 
235 typedef struct {
236 	thread_id		thread;
237 	team_id			team;
238 	char			name[B_OS_NAME_LENGTH];
239 	thread_state	state;
240 	int32			priority;
241 	sem_id			sem;
242 	bigtime_t		user_time;
243 	bigtime_t		kernel_time;
244 	void			*stack_base;
245 	void			*stack_end;
246 } thread_info;
247 
248 #define B_IDLE_PRIORITY					0
249 #define B_LOWEST_ACTIVE_PRIORITY		1
250 #define B_LOW_PRIORITY					5
251 #define B_NORMAL_PRIORITY				10
252 #define B_DISPLAY_PRIORITY				15
253 #define	B_URGENT_DISPLAY_PRIORITY		20
254 #define	B_REAL_TIME_DISPLAY_PRIORITY	100
255 #define	B_URGENT_PRIORITY				110
256 #define B_REAL_TIME_PRIORITY			120
257 
258 #define B_FIRST_REAL_TIME_PRIORITY		B_REAL_TIME_DISPLAY_PRIORITY
259 #define B_MIN_PRIORITY					B_IDLE_PRIORITY
260 #define B_MAX_PRIORITY					B_REAL_TIME_PRIORITY
261 
262 #define B_SYSTEM_TIMEBASE				0
263 
264 typedef int32 (*thread_func) (void *);
265 // the equivalent thread_entry has been removed (deprecated)
266 
267 extern thread_id	spawn_thread(thread_func, const char *name, int32 priority, void *data);
268 extern status_t		kill_thread(thread_id thread);
269 extern status_t		resume_thread(thread_id thread);
270 extern status_t		suspend_thread(thread_id thread);
271 
272 extern status_t		rename_thread(thread_id thread, const char *newName);
273 extern status_t		set_thread_priority (thread_id thread, int32 newPriority);
274 extern void			exit_thread(status_t status);
275 extern status_t		wait_for_thread (thread_id thread, status_t *threadReturnValue);
276 extern status_t		on_exit_thread(void (*callback)(void *), void *data);
277 
278 // ToDo: we also need the INTEL static inline here for compatibility (and the _kfind_thread_() function)
279 extern thread_id 	find_thread(const char *name);
280 
281 extern status_t		send_data(thread_id thread, int32 code, const void *buffer, size_t buffer_size);
282 extern status_t		receive_data(thread_id *sender, void *buffer, size_t buffer_size);
283 extern bool			has_data(thread_id thread);
284 
285 extern status_t		snooze(bigtime_t amount);
286 extern status_t		snooze_until(bigtime_t time, int timeBase);
287 
288 /* system private, use macros instead */
289 extern status_t		_get_thread_info(thread_id id, thread_info *info, size_t size);
290 extern status_t		_get_next_thread_info(team_id team, int32 *cookie, thread_info *info, size_t size);
291 
292 #define get_thread_info(id, info) \
293 			_get_thread_info((id), (info), sizeof(*(info)))
294 
295 #define get_next_thread_info(team, cookie, info) \
296 			_get_next_thread_info((team), (cookie), (info), sizeof(*(info)))
297 
298 
299 /*-------------------------------------------------------------*/
300 /* Time */
301 
302 extern uint32		real_time_clock(void);
303 extern void			set_real_time_clock(int32 secs_since_jan1_1970);
304 extern bigtime_t	real_time_clock_usecs(void);
305 extern status_t		set_timezone(char *timezone);
306 extern bigtime_t	system_time(void);     /* time since booting in microseconds */
307 
308 
309 /*-------------------------------------------------------------*/
310 /* Alarm */
311 
312 enum {
313 	B_ONE_SHOT_ABSOLUTE_ALARM	= 1,
314 	B_ONE_SHOT_RELATIVE_ALARM,
315 	B_PERIODIC_ALARM			/* "when" specifies the period */
316 };
317 
318 extern bigtime_t set_alarm(bigtime_t when, uint32 flags);
319 
320 
321 /*-------------------------------------------------------------*/
322 /* Debugger */
323 
324 extern void	debugger(const char *message);
325 
326 /*
327    calling this function with a non-zero value will cause your thread
328    to receive signals for any exceptional conditions that occur (i.e.
329    you'll get SIGSEGV for data access exceptions, SIGFPE for floating
330    point errors, SIGILL for illegal instructions, etc).
331 
332    to re-enable the default debugger pass a zero.
333 */
334 extern const int disable_debugger(int state);
335 
336 
337 /*-------------------------------------------------------------*/
338 /* System information */
339 
340 #if __INTEL__
341 #	define B_MAX_CPU_COUNT	8
342 #elif __POWERPC__
343 #	define B_MAX_CPU_COUNT	8
344 #endif
345 
346 typedef enum cpu_types {
347 	// ToDo: add latest models
348 
349 	/* Motorola/IBM */
350 	B_CPU_PPC_601						= 1,
351 	B_CPU_PPC_603						= 2,
352 	B_CPU_PPC_603e						= 3,
353 	B_CPU_PPC_604						= 4,
354 	B_CPU_PPC_604e						= 5,
355 	B_CPU_PPC_750   					= 6,
356 	B_CPU_PPC_686						= 13,
357 
358 	/* Intel */
359 	B_CPU_INTEL_X86						= 0x1000,
360 	B_CPU_INTEL_PENTIUM					= 0x1051,
361 	B_CPU_INTEL_PENTIUM75,
362 	B_CPU_INTEL_PENTIUM_486_OVERDRIVE,
363 	B_CPU_INTEL_PENTIUM_MMX,
364 	B_CPU_INTEL_PENTIUM_MMX_MODEL_4		= B_CPU_INTEL_PENTIUM_MMX,
365 	B_CPU_INTEL_PENTIUM_MMX_MODEL_8		= 0x1058,
366 	B_CPU_INTEL_PENTIUM75_486_OVERDRIVE,
367 	B_CPU_INTEL_PENTIUM_PRO				= 0x1061,
368 	B_CPU_INTEL_PENTIUM_II				= 0x1063,
369 	B_CPU_INTEL_PENTIUM_II_MODEL_3		= 0x1063,
370 	B_CPU_INTEL_PENTIUM_II_MODEL_5		= 0x1065,
371 	B_CPU_INTEL_CELERON					= 0x1066,
372 	B_CPU_INTEL_PENTIUM_III				= 0x1067,
373 	B_CPU_INTEL_PENTIUM_III_MODEL_8		= 0x1068,
374 
375 	/* AMD */
376 	B_CPU_AMD_X86						= 0x1100,
377 	B_CPU_AMD_K5_MODEL0					= 0x1150,
378 	B_CPU_AMD_K5_MODEL1,
379 	B_CPU_AMD_K5_MODEL2,
380 	B_CPU_AMD_K5_MODEL3,
381 	B_CPU_AMD_K6_MODEL6					= 0x1156,
382 	B_CPU_AMD_K6_MODEL7					= 0x1157,
383 	B_CPU_AMD_K6_MODEL8					= 0x1158,
384 	B_CPU_AMD_K6_2						= 0x1158,
385 	B_CPU_AMD_K6_MODEL9					= 0x1159,
386 	B_CPU_AMD_K6_III					= 0x1159,
387 
388 	B_CPU_AMD_ATHLON_MODEL1				= 0x1161,
389 
390 	/* VIA */
391 	B_CPU_CYRIX_X86						= 0x1200,
392 	B_CPU_CYRIX_GXm						= 0x1254,
393 	B_CPU_CYRIX_6x86MX					= 0x1260,
394 
395 	/* others */
396 	B_CPU_IDT_X86						= 0x1300,
397 	B_CPU_IDT_WINCHIP_C6				= 0x1354,
398 	B_CPU_IDT_WINCHIP_2					= 0x1358,
399 
400 	B_CPU_RISE_X86						= 0x1400,
401 	B_CPU_RISE_mP6						= 0x1450,
402 
403 	/* For compatibility */
404 	B_CPU_AMD_29K						= 14,
405 	B_CPU_X86,
406 	B_CPU_MC6502,
407 	B_CPU_Z80,
408 	B_CPU_ALPHA,
409 	B_CPU_MIPS,
410 	B_CPU_HPPA,
411 	B_CPU_M68K,
412 	B_CPU_ARM,
413 	B_CPU_SH,
414 	B_CPU_SPARC,
415 } cpu_type;
416 
417 #define B_CPU_X86_VENDOR_MASK	0x1F00
418 
419 #ifdef __INTEL__
420 typedef union {
421 	struct {
422 		uint32	max_eax;
423 		char	vendorid[12];
424 	} eax_0;
425 
426 	struct {
427 		uint32	stepping	: 4;
428 		uint32	model		: 4;
429 		uint32	family		: 4;
430 		uint32	type		: 2;
431 		uint32	reserved_0	: 18;
432 
433 		uint32	reserved_1;
434 		uint32	features;
435 		uint32	reserved_2;
436 	} eax_1;
437 
438 struct {
439 		uint8	call_num;
440 		uint8	cache_descriptors[15];
441 	} eax_2;
442 
443 	struct {
444 		uint32	reserved[2];
445 		uint32	serial_number_high;
446 		uint32	serial_number_low;
447 	} eax_3;
448 
449 	char		as_chars[16];
450 
451 	struct {
452 		uint32	eax;
453 		uint32	ebx;
454 		uint32	edx;
455 		uint32	ecx;
456 	} regs;
457 } cpuid_info;
458 
459 extern status_t get_cpuid(cpuid_info* info, uint32 eax_register, uint32 cpu_num);
460 #endif
461 
462 
463 typedef enum platform_types {
464 	B_BEBOX_PLATFORM = 0,
465 	B_MAC_PLATFORM,
466 	B_AT_CLONE_PLATFORM,
467 	B_ENIAC_PLATFORM,
468 	B_APPLE_II_PLATFORM,
469 	B_CRAY_PLATFORM,
470 	B_LISA_PLATFORM,
471 	B_TI_994A_PLATFORM,
472 	B_TIMEX_SINCLAIR_PLATFORM,
473 	B_ORAC_1_PLATFORM,
474 	B_HAL_PLATFORM,
475 	B_BESM_6_PLATFORM,
476 	B_MK_61_PLATFORM,
477 	B_NINTENDO_64_PLATFORM
478 } platform_type;
479 
480 typedef struct {
481 	bigtime_t	active_time;	/* usec of doing useful work since boot */
482 } cpu_info;
483 
484 
485 typedef int32 machine_id[2];	/* unique machine ID */
486 
487 typedef struct {
488 	machine_id		id;							/* unique machine ID */
489 	bigtime_t		boot_time;					/* time of boot (usecs since 1/1/1970) */
490 
491 	int32			cpu_count;					/* number of cpus */
492 	enum cpu_types	cpu_type;					/* type of cpu */
493 	int32			cpu_revision;				/* revision # of cpu */
494 	cpu_info		cpu_infos[B_MAX_CPU_COUNT];	/* info about individual cpus */
495 	int64			cpu_clock_speed;	 		/* processor clock speed (Hz) */
496 	int64			bus_clock_speed;			/* bus clock speed (Hz) */
497 	enum platform_types platform_type;          /* type of machine we're on */
498 
499 	int32			max_pages;					/* total # physical pages */
500 	int32			used_pages;					/* # physical pages in use */
501 	int32			page_faults;				/* # of page faults */
502 	int32			max_sems;
503 	int32			used_sems;
504 	int32			max_ports;
505 	int32			used_ports;
506 	int32			max_threads;
507 	int32			used_threads;
508 	int32			max_teams;
509 	int32			used_teams;
510 
511 //	ToDo: B_FILE_NAME_LENGTH is currently not defined at this point
512 //	char			kernel_name[B_FILE_NAME_LENGTH];		/* name of kernel */
513 	char			kernel_name[256];
514 	char			kernel_build_date[B_OS_NAME_LENGTH];	/* date kernel built */
515 	char			kernel_build_time[B_OS_NAME_LENGTH];	/* time kernel built */
516 	int64			kernel_version;             /* version of this kernel */
517 
518 	bigtime_t		_busy_wait_time;			/* reserved for Be */
519 	int32			pad[4];   	               	/* just in case... */
520 } system_info;
521 
522 /* system private, use macro instead */
523 extern status_t _get_system_info (system_info *returned_info, size_t size);
524 
525 #define get_system_info(info) \
526 			_get_system_info((info), sizeof(*(info)))
527 
528 extern int32	is_computer_on(void);
529 extern double	is_computer_on_fire(void);
530 
531 #ifdef __cplusplus
532 }
533 #endif
534 
535 #endif /* _OS_H */
536