xref: /haiku/headers/os/kernel/OS.h (revision 81f5654c124bf46fba0fd251f208e2d88d81e1ce)
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_CURRENT_TEAM	0
191 #define B_SYSTEM_TEAM	2
192 
193 extern status_t kill_team(team_id team);
194 	/* see also: send_signal() */
195 
196 /* system private, use macros instead */
197 extern status_t _get_team_info(team_id id, team_info *info, size_t size);
198 extern status_t _get_next_team_info(int32 *cookie, team_info *info, size_t size);
199 
200 #define get_team_info(id, info) \
201 			_get_team_info((id), (info), sizeof(*(info)))
202 
203 #define get_next_team_info(cookie, info) \
204 			_get_next_team_info((cookie), (info), sizeof(*(info)))
205 
206 /* team usage info */
207 
208 typedef struct {
209 	bigtime_t		user_time;
210 	bigtime_t		kernel_time;
211 } team_usage_info;
212 
213 /* system private, use macros instead */
214 extern status_t	_get_team_usage_info(team_id team, int32 who, team_usage_info *tui, size_t size);
215 
216 #define get_team_usage_info(team, who, info) \
217 			_get_team_usage_info((team), (who), (info), sizeof(*(info)))
218 
219 /*-------------------------------------------------------------*/
220 /* Threads */
221 
222 typedef enum {
223 	B_THREAD_RUNNING	= 1,
224 	B_THREAD_READY,
225 	B_THREAD_RECEIVING,
226 	B_THREAD_ASLEEP,
227 	B_THREAD_SUSPENDED,
228 	B_THREAD_WAITING
229 } thread_state;
230 
231 typedef struct {
232 	thread_id		thread;
233 	team_id			team;
234 	char			name[B_OS_NAME_LENGTH];
235 	thread_state	state;
236 	int32			priority;
237 	sem_id			sem;
238 	bigtime_t		user_time;
239 	bigtime_t		kernel_time;
240 	void			*stack_base;
241 	void			*stack_end;
242 } thread_info;
243 
244 #define B_IDLE_PRIORITY					0
245 #define B_LOWEST_ACTIVE_PRIORITY		1
246 #define B_LOW_PRIORITY					5
247 #define B_NORMAL_PRIORITY				10
248 #define B_DISPLAY_PRIORITY				15
249 #define	B_URGENT_DISPLAY_PRIORITY		20
250 #define	B_REAL_TIME_DISPLAY_PRIORITY	100
251 #define	B_URGENT_PRIORITY				110
252 #define B_REAL_TIME_PRIORITY			120
253 
254 #define B_FIRST_REAL_TIME_PRIORITY		B_REAL_TIME_DISPLAY_PRIORITY
255 #define B_MIN_PRIORITY					B_IDLE_PRIORITY
256 #define B_MAX_PRIORITY					B_REAL_TIME_PRIORITY
257 
258 #define B_SYSTEM_TIMEBASE				0
259 
260 typedef int32 (*thread_func) (void *);
261 #define thread_entry thread_func /* thread_entry is for backward compatibility only! Use thread_func */
262 
263 extern thread_id	spawn_thread(thread_func, const char *name, int32 priority, void *data);
264 extern status_t		kill_thread(thread_id thread);
265 extern status_t		resume_thread(thread_id thread);
266 extern status_t		suspend_thread(thread_id thread);
267 
268 extern status_t		rename_thread(thread_id thread, const char *newName);
269 extern status_t		set_thread_priority (thread_id thread, int32 newPriority);
270 extern void			exit_thread(status_t status);
271 extern status_t		wait_for_thread (thread_id thread, status_t *threadReturnValue);
272 extern status_t		on_exit_thread(void (*callback)(void *), void *data);
273 
274 #if __INTEL__ && !_KERNEL_MODE && !_NO_INLINE_ASM
275 static inline thread_id
276 find_thread(const char *name) {
277 	extern thread_id _kfind_thread_(const char *name);
278 	if (!name) {
279 		thread_id thread;
280 		__asm__ __volatile__ (
281 			"movl	%%fs:4, %%eax \n\t"
282 			: "=a"(thread) );
283 		return thread;
284 	}
285 	return _kfind_thread_(name);
286 }
287 #else
288 extern thread_id 	find_thread(const char *name);
289 #endif
290 
291 extern status_t		send_data(thread_id thread, int32 code, const void *buffer, size_t buffer_size);
292 extern status_t		receive_data(thread_id *sender, void *buffer, size_t buffer_size);
293 extern bool			has_data(thread_id thread);
294 
295 extern status_t		snooze(bigtime_t amount);
296 extern status_t		snooze_until(bigtime_t time, int timeBase);
297 
298 /* system private, use macros instead */
299 extern status_t		_get_thread_info(thread_id id, thread_info *info, size_t size);
300 extern status_t		_get_next_thread_info(team_id team, int32 *cookie, thread_info *info, size_t size);
301 
302 #define get_thread_info(id, info) \
303 			_get_thread_info((id), (info), sizeof(*(info)))
304 
305 #define get_next_thread_info(team, cookie, info) \
306 			_get_next_thread_info((team), (cookie), (info), sizeof(*(info)))
307 
308 
309 /*-------------------------------------------------------------*/
310 /* Time */
311 
312 extern uint32		real_time_clock(void);
313 extern void			set_real_time_clock(uint32 secs_since_jan1_1970);
314 extern bigtime_t	real_time_clock_usecs(void);
315 extern status_t		set_timezone(char *timezone);
316 extern bigtime_t	system_time(void);     /* time since booting in microseconds */
317 
318 
319 /*-------------------------------------------------------------*/
320 /* Alarm */
321 
322 enum {
323 	B_ONE_SHOT_ABSOLUTE_ALARM	= 1,
324 	B_ONE_SHOT_RELATIVE_ALARM,
325 	B_PERIODIC_ALARM			/* "when" specifies the period */
326 };
327 
328 extern bigtime_t set_alarm(bigtime_t when, uint32 flags);
329 
330 
331 /*-------------------------------------------------------------*/
332 /* Debugger */
333 
334 extern void	debugger(const char *message);
335 
336 /*
337    calling this function with a non-zero value will cause your thread
338    to receive signals for any exceptional conditions that occur (i.e.
339    you'll get SIGSEGV for data access exceptions, SIGFPE for floating
340    point errors, SIGILL for illegal instructions, etc).
341 
342    to re-enable the default debugger pass a zero.
343 */
344 extern const int disable_debugger(int state);
345 
346 
347 /*-------------------------------------------------------------*/
348 /* System information */
349 
350 #if __INTEL__
351 #	define B_MAX_CPU_COUNT	8
352 #elif __POWERPC__
353 #	define B_MAX_CPU_COUNT	8
354 #endif
355 
356 #define OBOS_CPU_TYPES
357 
358 typedef enum cpu_types {
359 	// ToDo: add latest models
360 
361 	/* Motorola/IBM */
362 	B_CPU_PPC_601						= 1,
363 	B_CPU_PPC_603						= 2,
364 	B_CPU_PPC_603e						= 3,
365 	B_CPU_PPC_604						= 4,
366 	B_CPU_PPC_604e						= 5,
367 	B_CPU_PPC_750   					= 6,
368 	B_CPU_PPC_686						= 13,
369 
370 	/* Intel */
371 	B_CPU_INTEL_X86						= 0x1000,
372 	B_CPU_INTEL_PENTIUM					= 0x1051,
373 	B_CPU_INTEL_PENTIUM75,
374 	B_CPU_INTEL_PENTIUM_486_OVERDRIVE,
375 	B_CPU_INTEL_PENTIUM_MMX,
376 	B_CPU_INTEL_PENTIUM_MMX_MODEL_4		= B_CPU_INTEL_PENTIUM_MMX,
377 	B_CPU_INTEL_PENTIUM_MMX_MODEL_8		= 0x1058,
378 	B_CPU_INTEL_PENTIUM75_486_OVERDRIVE,
379 	B_CPU_INTEL_PENTIUM_PRO				= 0x1061,
380 	B_CPU_INTEL_PENTIUM_II				= 0x1063,
381 	B_CPU_INTEL_PENTIUM_II_MODEL_3		= 0x1063,
382 	B_CPU_INTEL_PENTIUM_II_MODEL_5		= 0x1065,
383 	B_CPU_INTEL_CELERON					= 0x1066,
384 	B_CPU_INTEL_PENTIUM_III				= 0x1067,
385 	B_CPU_INTEL_PENTIUM_III_MODEL_8		= 0x1068,
386 #ifdef OBOS_CPU_TYPES
387 	B_CPU_INTEL_PENTIUM_III_MODEL_11 	= 0x106b,
388 	B_CPU_INTEL_PENTIUM_IV				= 0x10f0,
389 	B_CPU_INTEL_PENTIUM_IV_MODEL1,
390 	B_CPU_INTEL_PENTIUM_IV_MODEL2,
391 	B_CPU_INTEL_PENTIUM_IV_XEON 		= 0x0F27,
392 #endif
393 
394 	/* AMD */
395 	B_CPU_AMD_X86						= 0x1100,
396 	B_CPU_AMD_K5_MODEL0					= 0x1150,
397 	B_CPU_AMD_K5_MODEL1,
398 	B_CPU_AMD_K5_MODEL2,
399 	B_CPU_AMD_K5_MODEL3,
400 	B_CPU_AMD_K6_MODEL6					= 0x1156,
401 	B_CPU_AMD_K6_MODEL7					= 0x1157,
402 	B_CPU_AMD_K6_MODEL8					= 0x1158,
403 	B_CPU_AMD_K6_2						= 0x1158,
404 	B_CPU_AMD_K6_MODEL9					= 0x1159,
405 	B_CPU_AMD_K6_III					= 0x1159,
406 #ifdef OBOS_CPU_TYPES
407 	B_CPU_AMD_K6_III_MODEL2				= 0x115D,
408 #endif
409 
410 	B_CPU_AMD_ATHLON_MODEL1				= 0x1161,
411 #ifdef OBOS_CPU_TYPES
412 	B_CPU_AMD_ATHLON_MODEL2 			= 0x1162,
413 
414 	B_CPU_AMD_DURON 					= 0x1163,
415 
416 	B_CPU_AMD_ATHLON_THUNDERBIRD		= 0x1164,
417 	B_CPU_AMD_ATHLON_XP 				= 0x1166,
418 	B_CPU_AMD_ATHLON_XP_MODEL2,
419 	B_CPU_AMD_ATHLON_XP_MODEL3,
420 	B_CPU_AMD_ATHLON_XP_MODEL_BARTON 	= 0x116A,
421 #endif
422 
423 	/* VIA */
424 	B_CPU_CYRIX_X86						= 0x1200,
425 	B_CPU_CYRIX_GXm						= 0x1254,
426 	B_CPU_CYRIX_6x86MX					= 0x1260,
427 
428 	/* others */
429 	B_CPU_IDT_X86						= 0x1300,
430 	B_CPU_IDT_WINCHIP_C6				= 0x1354,
431 	B_CPU_IDT_WINCHIP_2					= 0x1358,
432 
433 	B_CPU_RISE_X86						= 0x1400,
434 	B_CPU_RISE_mP6						= 0x1450,
435 
436 #ifdef OBOS_CPU_TYPES
437 	B_CPU_NATIONAL_X86					= 0x1500,
438 	B_CPU_NATIONAL_GEODE_GX1			= 0x1554,
439 #endif
440 
441 	/* For compatibility */
442 	B_CPU_AMD_29K						= 14,
443 	B_CPU_X86,
444 	B_CPU_MC6502,
445 	B_CPU_Z80,
446 	B_CPU_ALPHA,
447 	B_CPU_MIPS,
448 	B_CPU_HPPA,
449 	B_CPU_M68K,
450 	B_CPU_ARM,
451 	B_CPU_SH,
452 	B_CPU_SPARC,
453 } cpu_type;
454 
455 #define B_CPU_X86_VENDOR_MASK	0x1F00
456 
457 #ifdef __INTEL__
458 typedef union {
459 	struct {
460 		uint32	max_eax;
461 		char	vendorid[12];
462 	} eax_0;
463 
464 	struct {
465 		uint32	stepping	: 4;
466 		uint32	model		: 4;
467 		uint32	family		: 4;
468 		uint32	type		: 2;
469 		uint32	reserved_0	: 18;
470 
471 		uint32	reserved_1;
472 		uint32	features;
473 		uint32	reserved_2;
474 	} eax_1;
475 
476 	struct {
477 		uint8	call_num;
478 		uint8	cache_descriptors[15];
479 	} eax_2;
480 
481 	struct {
482 		uint32	reserved[2];
483 		uint32	serial_number_high;
484 		uint32	serial_number_low;
485 	} eax_3;
486 
487 	char		as_chars[16];
488 
489 	struct {
490 		uint32	eax;
491 		uint32	ebx;
492 		uint32	edx;
493 		uint32	ecx;
494 	} regs;
495 } cpuid_info;
496 
497 extern status_t get_cpuid(cpuid_info* info, uint32 eax_register, uint32 cpu_num);
498 #endif
499 
500 
501 typedef enum platform_types {
502 	B_BEBOX_PLATFORM = 0,
503 	B_MAC_PLATFORM,
504 	B_AT_CLONE_PLATFORM,
505 	B_ENIAC_PLATFORM,
506 	B_APPLE_II_PLATFORM,
507 	B_CRAY_PLATFORM,
508 	B_LISA_PLATFORM,
509 	B_TI_994A_PLATFORM,
510 	B_TIMEX_SINCLAIR_PLATFORM,
511 	B_ORAC_1_PLATFORM,
512 	B_HAL_PLATFORM,
513 	B_BESM_6_PLATFORM,
514 	B_MK_61_PLATFORM,
515 	B_NINTENDO_64_PLATFORM
516 } platform_type;
517 
518 typedef struct {
519 	bigtime_t	active_time;	/* usec of doing useful work since boot */
520 } cpu_info;
521 
522 
523 typedef int32 machine_id[2];	/* unique machine ID */
524 
525 typedef struct {
526 	machine_id		id;							/* unique machine ID */
527 	bigtime_t		boot_time;					/* time of boot (usecs since 1/1/1970) */
528 
529 	int32			cpu_count;					/* number of cpus */
530 	enum cpu_types	cpu_type;					/* type of cpu */
531 	int32			cpu_revision;				/* revision # of cpu */
532 	cpu_info		cpu_infos[B_MAX_CPU_COUNT];	/* info about individual cpus */
533 	int64			cpu_clock_speed;	 		/* processor clock speed (Hz) */
534 	int64			bus_clock_speed;			/* bus clock speed (Hz) */
535 	enum platform_types platform_type;          /* type of machine we're on */
536 
537 	int32			max_pages;					/* total # physical pages */
538 	int32			used_pages;					/* # physical pages in use */
539 	int32			page_faults;				/* # of page faults */
540 	int32			max_sems;
541 	int32			used_sems;
542 	int32			max_ports;
543 	int32			used_ports;
544 	int32			max_threads;
545 	int32			used_threads;
546 	int32			max_teams;
547 	int32			used_teams;
548 
549 //	ToDo: B_FILE_NAME_LENGTH is currently not defined at this point
550 //	char			kernel_name[B_FILE_NAME_LENGTH];		/* name of kernel */
551 	char			kernel_name[256];
552 	char			kernel_build_date[B_OS_NAME_LENGTH];	/* date kernel built */
553 	char			kernel_build_time[B_OS_NAME_LENGTH];	/* time kernel built */
554 	int64			kernel_version;             /* version of this kernel */
555 
556 	bigtime_t		_busy_wait_time;			/* reserved for Be */
557 	int32			pad[4];   	               	/* just in case... */
558 } system_info;
559 
560 /* system private, use macro instead */
561 extern status_t _get_system_info (system_info *returned_info, size_t size);
562 
563 #define get_system_info(info) \
564 			_get_system_info((info), sizeof(*(info)))
565 
566 extern int32	is_computer_on(void);
567 extern double	is_computer_on_fire(void);
568 
569 #ifdef __cplusplus
570 }
571 #endif
572 
573 #endif /* _OS_H */
574