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