1 /*---------------------------------------------------------------------------*
2   Project:     Cafe OS
3   File:        os.h
4 
5   Copyright (C) Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13  *---------------------------------------------------------------------------*/
14 
15 #ifndef __OS_H__
16 #define __OS_H__
17 
18 #ifndef _ASSEMBLER
19 
20 #include <types.h>
21 #include <ppc_upid.h>
22 
23 #include <cafe/base/ppc_asm_user.h>
24 #include <stdarg.h>
25 #ifdef __ghs__
26 #include <ppc_ghs.h>
27 #endif
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /*---------------------------------------------------------------------------*
34     Round APIs
35  *---------------------------------------------------------------------------*/
36 
37 // Most subsystems require 32 byte alignment
38 #define OSRoundUp32B(x)       (((u32)(x) + 32 - 1) & ~(32 - 1))
39 #define OSRoundDown32B(x)     (((u32)(x)) & ~(32 - 1))
40 
41 /*---------------------------------------------------------------------------*
42     Basic OS API
43  *---------------------------------------------------------------------------*/
44 
45 /*---------------------------------------------------------------------------*
46   Name:         OSGetConsoleType
47 
48   Description:  Checks the console type.
49 
50   Arguments:    None.
51 
52   Returns:      Returns one of the OS_CONSOLE_* values
53 
54  *---------------------------------------------------------------------------*/
55 u32     OSGetConsoleType( void );
56 
57 /*---------------------------------------------------------------------------*
58   Name:         OSGetSecurityLevel
59 
60   Description:  Get the security level.
61 
62   Arguments:    None.
63 
64   Returns:      OS_SECURITY_LEVEL_PROD or OS_SECURITY_LEVEL_DEV
65 
66  *---------------------------------------------------------------------------*/
67 
68 #define	OS_SECURITY_LEVEL_PROD	0
69 #define	OS_SECURITY_LEVEL_DEV	1
70 
71 u32     OSGetSecurityLevel( void );
72 
73 /*---------------------------------------------------------------------------*
74   Name:         OSIsDebuggerPresent
75 
76   Description:  Indicate if debugger is initialized and connected (open
77   				and running).
78 
79   Arguments:    None.
80 
81   Returns:      TRUE if debugger is connected
82                 FALSE if debugger is not connected
83 
84  *---------------------------------------------------------------------------*/
85 BOOL    OSIsDebuggerPresent( void );
86 
87 /*---------------------------------------------------------------------------*
88   Name:         OSIsDebuggerInitialized
89 
90   Description:  Indicate if debugger is initialized but not necessarily
91 				connected (open and running).
92 
93 				When the debugger is initialized you can call OS debug
94 				functions like OSDebug() or OSDebugStr().
95 
96 				Calling a OS debug function when OSIsDebuggerInitialized() is
97 				true, and OSIsDebuggerPresent() is false, will cause the
98 				process to stop and wait for the debugger to connect.
99 
100 				Calling any OS debug function when OSIsDebuggerInitialized is
101 				false will cause a fatal unhandled exception.
102 
103   Arguments:    None.
104 
105   Returns:      TRUE if debugger is initialized
106                 FALSE if debugger is not initialized
107 
108  *---------------------------------------------------------------------------*/
109 BOOL    OSIsDebuggerInitialized( void );
110 
111 
112 /* foreground switching */
113 void OSSavesDone_ReadyToRelease(void);
114 void OSReleaseForeground( void );
115 BOOL OSGetForegroundBucket( void ** appRetArea, u32 * apRetSizeBytes);
116 BOOL OSGetForegroundBucketFreeArea( void ** appRetArea, u32 * apRetSizeBytes);
117 BOOL OSRequestFastExit(u32 aFlags, BOOL bRunExit);
118 BOOL OSSendAppSwitchRequest(UPID aProcId, void *apArgs, u32 aArgsBytes);
119 BOOL OSGetCallArgs(UPID *apRetCaller, u8 *apBuffer, u32 aBufferBytes);
120 BOOL OSSetScreenCapturePermission(BOOL enabled);
121 BOOL OSGetScreenCapturePermission(void);
122 BOOL OSSetScreenCapturePermissionEx(BOOL tvEnabled, BOOL drcEnabled);
123 BOOL OSGetScreenCapturePermissionEx(BOOL* tvEnabled, BOOL* drcEnabled);
124 BOOL OSEnableHomeButtonMenu(BOOL enable);
125 BOOL OSIsColdBoot(void);
126 BOOL OSIsSelfRefreshBoot(void);
127 BOOL OSIsNormalBoot(void);
128 BOOL OSIsECOBoot(void);
129 BOOL OSIsStandbyBoot(void);
130 BOOL OSIsOffBoot();
131 BOOL OSIsCompatBoot(void);
132 u32  OSGetBootPMFlags(void);
133 u32  OSGetLastPMState(void);
134 u32  OSGetCurrentPMState(void);
135 BOOL OSIsProdMode(void);
136 BOOL OSIsECOMode(void);
137 BOOL OSIsHomeButtonMenuEnabled(void);
138 void OSEnableForegroundExit(void);
139 
140 #define APP_IN_FOREGROUND (OSGetForegroundBucket(NULL, NULL))
141 
142 /* system policy notifications */
143 typedef enum _osPolicyRequest
144 {
145     OSPOLICY_Exit=1,
146     OSPOLICY_NotifyNetIoStart=2,
147     OSPOLICY_NotifyNetIoEnd=3,
148     OSPOLICY_NotifyHomeButtonDenied=4,
149 } OSPolicyRequest;
150 BOOL OSSendPolicyRequest(OSPolicyRequest aPolicy, UPID aTargetProcId);
151 
152 void    OSRegisterVersion( const char* id );
153 
154 void    OSGetArgcArgv(int *apRetArgc, char const ***apRetArgv);
155 
156 /*---------------------------------------------------------------------------*
157     Real-time Clock API
158  *---------------------------------------------------------------------------*/
159 
160 #include <cafe/os/OSTime.h>
161 
162 /*----------------------------------------------------------------------*
163     Debug API
164  *----------------------------------------------------------------------*/
165 
166 #define OSHalt(msg)             OSPanic(__FILE__, __LINE__, msg)
167 
168 #ifdef _DEBUG
169 
170 #ifndef ASSERT
171 #define ASSERT(exp)                                             \
172     (void) ((exp) ||                                            \
173             (OSPanic(__FILE__, __LINE__, "Failed assertion " #exp), 0))
174 #endif
175 
176 #ifndef ASSERTMSG
177 #if defined(__STDC_VERSION__) && (199901L <= __STDC_VERSION__) || defined(__ghs__)
178 #define ASSERTMSG(exp, ...)                                     \
179     (void) ((exp) ||                                            \
180             (OSPanic(__FILE__, __LINE__, __VA_ARGS__), 0))
181 #else
182 #define ASSERTMSG(exp, msg)                                     \
183     (void) ((exp) ||                                            \
184             (OSPanic(__FILE__, __LINE__, (msg)), 0))
185 #endif
186 #endif
187 
188 #ifndef ASSERTMSG1
189 #define ASSERTMSG1(exp, msg, param1)                            \
190     (void) ((exp) ||                                            \
191             (OSPanic(__FILE__, __LINE__, (msg),                 \
192                      (param1)), 0))
193 #endif
194 
195 #ifndef ASSERTMSG2
196 #define ASSERTMSG2(exp, msg, param1, param2)                    \
197     (void) ((exp) ||                                            \
198             (OSPanic(__FILE__, __LINE__, (msg),                 \
199                      (param1), (param2)), 0))
200 #endif
201 
202 #ifndef ASSERTMSG3
203 #define ASSERTMSG3(exp, msg, param1, param2, param3)            \
204     (void) ((exp) ||                                            \
205             (OSPanic(__FILE__, __LINE__, (msg),                 \
206                      (param1), (param2), (param3)), 0))
207 #endif
208 
209 #ifndef ASSERTMSG4
210 #define ASSERTMSG4(exp, msg, param1, param2, param3, param4)    \
211     (void) ((exp) ||                                            \
212             (OSPanic(__FILE__, __LINE__, (msg),                 \
213                      (param1), (param2), (param3), (param4)), 0))
214 #endif
215 
216 
217 #else   // _DEBUG
218 
219 #ifndef ASSERT
220 #define ASSERT(exp)                                             ((void) 0)
221 #endif
222 
223 #ifndef ASSERTMSG
224 #if defined(__STDC_VERSION__) && (199901L <= __STDC_VERSION__) || defined(__ghs__)
225 #define ASSERTMSG(exp, ...)                                     ((void) 0)
226 #else
227 #define ASSERTMSG(exp, msg)                                     ((void) 0)
228 #endif
229 #endif
230 
231 #ifndef ASSERTMSG1
232 #define ASSERTMSG1(exp, msg, param1)                            ((void) 0)
233 #endif
234 #ifndef ASSERTMSG2
235 #define ASSERTMSG2(exp, msg, param1, param2)                    ((void) 0)
236 #endif
237 #ifndef ASSERTMSG3
238 #define ASSERTMSG3(exp, msg, param1, param2, param3)            ((void) 0)
239 #endif
240 #ifndef ASSERTMSG4
241 #define ASSERTMSG4(exp, msg, param1, param2, param3, param4)    ((void) 0)
242 #endif
243 
244 #endif  // _DEBUG
245 
246 #ifndef OS_STATIC_ASSERT
247     #define OS_STATIC_ASSERT(e, message)   \
248         typedef int os_static_assert__failed__##message[!(e) ? -1 : 1]
249 #endif
250 
251 void OSPanic  ( const char* file, int line, const char* msg, ... );
252 void OSReport ( const char* msg, ... );
253 void OSVReport( const char* msg, va_list list );
254 void OSConsoleWrite( const char *buf, unsigned long len );
255 void OSFatal  ( const char* msg );
256 void OSReportSetNewline(const char *newline);
257 void OSSupressConsoleOutput(BOOL umSuppress, BOOL appSuppress, BOOL kmSuppress);
258 void OSDisableUserHeartbeat(void);
259 
260 /*
261  * caferun -v <n> : set verbose level (0=off 7 == MAX) determines whether these
262  * versions of OSReport emit anything or are skipped.
263  */
264 void OSReportWarn( const char* msg, ...);
265 void OSReportInfo( const char* msg, ...);
266 void OSReportVerbose( const char* msg, ...);
267 
268 #if INCLUDE_COSREPORT
269 #include "private/COSReport.h"
270 extern void OSConsoleWriteForModule(COSREPORT_MODULE mod, COSREPORT_LEVEL level, const char *buffer, unsigned long count);
271 #endif
272 
273 #ifdef __ghs__
274 /* made this macros to avoid problems if inlining is off
275  * note: do { ... } while (0)
276  * executes body of loop exactly once, and avoids problems
277  * if expanded in an if statement like:
278  * if (x != 0) OSDebugStr("failed");
279  */
280 #define OSDebugStr(message) do { \
281 	__SETREG(3, (unsigned int) message); \
282 	asm("tw 31,r31,r31"); \
283 } while (0)
284 #define OSDebug() OSDebugStr(0)
285 
286 #define OSDebugMessageBytes(message, count) do { \
287 	__SETREG(3, (unsigned int) message); \
288 	__SETREG(4, count); \
289 	asm("tw 31,r30,r30"); \
290 } while (0)
291 #define OSDebugMessage(message) OSDebugMessageBytes(message, 0)
292 #define OSDebugStrIfConnected(message) do { if (OSIsDebuggerPresent()) OSDebugStr(message); } while (0)
293 #else
294 extern void OSDebug(void);
295 extern void OSDebugStr(const char *message);
296 extern void OSDebugMessage(const char *message);
297 extern void OSDebugMessageBytes(const char *message, unsigned int count);
298 extern void OSDebugStrIfConnected(const char *message);
299 #endif
300 
301 /*---------------------------------------------------------------------------*
302     Get infomation APIs
303  *---------------------------------------------------------------------------*/
304 
305 const char* OSGetAppGamename(void);
306 const u8    OSGetAppType(void);
307 #define  OS_APP_TYPE_WC24             0x20
308 #define  OS_APP_TYPE_IPL              0x40
309 #define  OS_APP_TYPE_DVD              0x80
310 #define  OS_APP_TYPE_NAND             0x81
311 
312 #ifdef __cplusplus
313 }
314 #endif
315 
316 /*---------------------------------------------------------------------------*
317     Advanced OS APIs
318  *---------------------------------------------------------------------------*/
319 
320 #include <cafe/os/OSCore.h>      // Processor Core routines and defines
321 #include <cafe/os/OSSync.h>      // Synchronization routines and defines
322 #include <cafe/os/OSAlarm.h>     // Alarm routines and defines
323 #include <cafe/os/OSAtomic.h>    // Atomic operation
324 #include <cafe/os/OSCache.h>     // Cache routines and defines
325 #include <cafe/os/OSContext.h>   // Context structures and defines
326 #include <cafe/os/OSDeviceThread.h> // Threads for device drivers and user callback functions
327 #include <cafe/os/OSError.h>     // Error handler routines and defines
328 #include <cafe/os/OSFastCast.h>  // Fast float/int conversion
329 #include <cafe/os/OSMessage.h>   // Message routines and defines
330 #include <cafe/os/OSMutex.h>     // Mutex routines and defines
331 #include <cafe/os/OSFastMutex.h> // Fast Mutex routines which are not thread cancel points
332 #include <cafe/os/OSSemaphore.h> // Semaphore routines and defines
333 #include <cafe/os/OSEvent.h>
334 #include <cafe/os/OSSystemInfo.h>
335 #include <cafe/os/OSThread.h>    // Thread routines and defines
336 #include <cafe/os/OSReset.h>     // Reset APIs
337 #include <cafe/os/OSDynLoad.h>
338 #include <cafe/os/OSFunctionType.h>
339 #include <cafe/os/OSPerformanceMonitor.h>
340 #include <cafe/os/OSLaunch.h>
341 #include <cafe/os/OSCoroutine.h>
342 #include <cafe/os/OSMemory.h>
343 #include <cafe/os/OSMemmap.h>    // Memory mapping APIs
344 #include <cafe/os/OSPlatform.h>  // Platform information
345 #include <cafe/os/OSSysHealth.h>
346 
347 #define WEAK_SYMBOL
348 
349 #endif // _ASSEMBLER
350 #endif  // __OS_H__
351 
352