xref: /haiku/headers/os/kernel/OS.h (revision ae17e5046d20aabb5a019905b528f462ecb7d64e)
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 typedef enum cpu_types {
342 	// ToDo: add latest models
343 
344 	/* Motorola/IBM */
345 	B_CPU_PPC_601						= 1,
346 	B_CPU_PPC_603						= 2,
347 	B_CPU_PPC_603e						= 3,
348 	B_CPU_PPC_604						= 4,
349 	B_CPU_PPC_604e						= 5,
350 	B_CPU_PPC_750   					= 6,
351 	B_CPU_PPC_686						= 13,
352 
353 	/* Intel */
354 	B_CPU_INTEL_X86						= 0x1000,
355 	B_CPU_INTEL_PENTIUM					= 0x1051,
356 	B_CPU_INTEL_PENTIUM75,
357 	B_CPU_INTEL_PENTIUM_486_OVERDRIVE,
358 	B_CPU_INTEL_PENTIUM_MMX,
359 	B_CPU_INTEL_PENTIUM_MMX_MODEL_4		= B_CPU_INTEL_PENTIUM_MMX,
360 	B_CPU_INTEL_PENTIUM_MMX_MODEL_8		= 0x1058,
361 	B_CPU_INTEL_PENTIUM75_486_OVERDRIVE,
362 	B_CPU_INTEL_PENTIUM_PRO				= 0x1061,
363 	B_CPU_INTEL_PENTIUM_II				= 0x1063,
364 	B_CPU_INTEL_PENTIUM_II_MODEL_3		= 0x1063,
365 	B_CPU_INTEL_PENTIUM_II_MODEL_5		= 0x1065,
366 	B_CPU_INTEL_CELERON					= 0x1066,
367 	B_CPU_INTEL_PENTIUM_III				= 0x1067,
368 	B_CPU_INTEL_PENTIUM_III_MODEL_8		= 0x1068,
369 
370 	/* AMD */
371 	B_CPU_AMD_X86						= 0x1100,
372 	B_CPU_AMD_K5_MODEL0					= 0x1150,
373 	B_CPU_AMD_K5_MODEL1,
374 	B_CPU_AMD_K5_MODEL2,
375 	B_CPU_AMD_K5_MODEL3,
376 	B_CPU_AMD_K6_MODEL6					= 0x1156,
377 	B_CPU_AMD_K6_MODEL7					= 0x1157,
378 	B_CPU_AMD_K6_MODEL8					= 0x1158,
379 	B_CPU_AMD_K6_2						= 0x1158,
380 	B_CPU_AMD_K6_MODEL9					= 0x1159,
381 	B_CPU_AMD_K6_III					= 0x1159,
382 
383 	B_CPU_AMD_ATHLON_MODEL1				= 0x1161,
384 	B_CPU_AMD_ATHLON_THUNDERBIRD			= 0x1164,
385 
386 	/* VIA */
387 	B_CPU_CYRIX_X86						= 0x1200,
388 	B_CPU_CYRIX_GXm						= 0x1254,
389 	B_CPU_CYRIX_6x86MX					= 0x1260,
390 
391 	/* others */
392 	B_CPU_IDT_X86						= 0x1300,
393 	B_CPU_IDT_WINCHIP_C6				= 0x1354,
394 	B_CPU_IDT_WINCHIP_2					= 0x1358,
395 
396 	B_CPU_RISE_X86						= 0x1400,
397 	B_CPU_RISE_mP6						= 0x1450,
398 
399 	B_CPU_NATIONAL_X86					= 0x1500,
400 	B_CPU_NATIONAL_GEODE_GX1				= 0x1554,
401 
402 	/* For compatibility */
403 	B_CPU_AMD_29K						= 14,
404 	B_CPU_X86,
405 	B_CPU_MC6502,
406 	B_CPU_Z80,
407 	B_CPU_ALPHA,
408 	B_CPU_MIPS,
409 	B_CPU_HPPA,
410 	B_CPU_M68K,
411 	B_CPU_ARM,
412 	B_CPU_SH,
413 	B_CPU_SPARC,
414 } cpu_type;
415 
416 #define B_CPU_X86_VENDOR_MASK	0x1F00
417 
418 #ifdef __INTEL__
419 typedef union {
420 	struct {
421 		uint32	max_eax;
422 		char	vendorid[12];
423 	} eax_0;
424 
425 	struct {
426 		uint32	stepping	: 4;
427 		uint32	model		: 4;
428 		uint32	family		: 4;
429 		uint32	type		: 2;
430 		uint32	reserved_0	: 18;
431 
432 		uint32	reserved_1;
433 		uint32	features;
434 		uint32	reserved_2;
435 	} eax_1;
436 
437 struct {
438 		uint8	call_num;
439 		uint8	cache_descriptors[15];
440 	} eax_2;
441 
442 	struct {
443 		uint32	reserved[2];
444 		uint32	serial_number_high;
445 		uint32	serial_number_low;
446 	} eax_3;
447 
448 	char		as_chars[16];
449 
450 	struct {
451 		uint32	eax;
452 		uint32	ebx;
453 		uint32	edx;
454 		uint32	ecx;
455 	} regs;
456 } cpuid_info;
457 
458 extern status_t get_cpuid(cpuid_info* info, uint32 eax_register, uint32 cpu_num);
459 #endif
460 
461 
462 typedef enum platform_types {
463 	B_BEBOX_PLATFORM = 0,
464 	B_MAC_PLATFORM,
465 	B_AT_CLONE_PLATFORM,
466 	B_ENIAC_PLATFORM,
467 	B_APPLE_II_PLATFORM,
468 	B_CRAY_PLATFORM,
469 	B_LISA_PLATFORM,
470 	B_TI_994A_PLATFORM,
471 	B_TIMEX_SINCLAIR_PLATFORM,
472 	B_ORAC_1_PLATFORM,
473 	B_HAL_PLATFORM,
474 	B_BESM_6_PLATFORM,
475 	B_MK_61_PLATFORM,
476 	B_NINTENDO_64_PLATFORM
477 } platform_type;
478 
479 typedef struct {
480 	bigtime_t	active_time;	/* usec of doing useful work since boot */
481 } cpu_info;
482 
483 
484 typedef int32 machine_id[2];	/* unique machine ID */
485 
486 typedef struct {
487 	machine_id		id;							/* unique machine ID */
488 	bigtime_t		boot_time;					/* time of boot (usecs since 1/1/1970) */
489 
490 	int32			cpu_count;					/* number of cpus */
491 	enum cpu_types	cpu_type;					/* type of cpu */
492 	int32			cpu_revision;				/* revision # of cpu */
493 	cpu_info		cpu_infos[B_MAX_CPU_COUNT];	/* info about individual cpus */
494 	int64			cpu_clock_speed;	 		/* processor clock speed (Hz) */
495 	int64			bus_clock_speed;			/* bus clock speed (Hz) */
496 	enum platform_types platform_type;          /* type of machine we're on */
497 
498 	int32			max_pages;					/* total # physical pages */
499 	int32			used_pages;					/* # physical pages in use */
500 	int32			page_faults;				/* # of page faults */
501 	int32			max_sems;
502 	int32			used_sems;
503 	int32			max_ports;
504 	int32			used_ports;
505 	int32			max_threads;
506 	int32			used_threads;
507 	int32			max_teams;
508 	int32			used_teams;
509 
510 //	ToDo: B_FILE_NAME_LENGTH is currently not defined at this point
511 //	char			kernel_name[B_FILE_NAME_LENGTH];		/* name of kernel */
512 	char			kernel_name[256];
513 	char			kernel_build_date[B_OS_NAME_LENGTH];	/* date kernel built */
514 	char			kernel_build_time[B_OS_NAME_LENGTH];	/* time kernel built */
515 	int64			kernel_version;             /* version of this kernel */
516 
517 	bigtime_t		_busy_wait_time;			/* reserved for Be */
518 	int32			pad[4];   	               	/* just in case... */
519 } system_info;
520 
521 /* system private, use macro instead */
522 extern status_t _get_system_info (system_info *returned_info, size_t size);
523 
524 #define get_system_info(info) \
525 			_get_system_info((info), sizeof(*(info)))
526 
527 extern int32	is_computer_on(void);
528 extern double	is_computer_on_fire(void);
529 
530 #ifdef __cplusplus
531 }
532 #endif
533 
534 #endif /* _OS_H */
535