1 /*---------------------------------------------------------------------------*
2   Project:     Cafe OS
3   File:        os.h
4 
5   Copyright 1998-2011 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 un-handled 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 
259 /*
260  * caferun -v <n> : set verbose level (0=off 7 == MAX) determines whether these
261  * versions of OSReport emit anything or are skipped.
262  */
263 void OSReportWarn( const char* msg, ...);
264 void OSReportInfo( const char* msg, ...);
265 void OSReportVerbose( const char* msg, ...);
266 
267 #if INCLUDE_COSREPORT
268 #include "private/COSReport.h"
269 extern void OSConsoleWriteForModule(COSREPORT_MODULE mod, COSREPORT_LEVEL level, const char *buffer, unsigned long count);
270 #endif
271 
272 #ifdef __ghs__
273 /* made this macros to avoid problems if inlining is off
274  * note: do { ... } while (0)
275  * executes body of loop exactly once, and avoids problems
276  * if expanded in an if statement like:
277  * if (x != 0) OSDebugStr("failed");
278  */
279 #define OSDebugStr(message) do { \
280 	__SETREG(3, (unsigned int) message); \
281 	asm("tw 31,r31,r31"); \
282 } while (0)
283 #define OSDebug() OSDebugStr(0)
284 
285 #define OSDebugMessageBytes(message, count) do { \
286 	__SETREG(3, (unsigned int) message); \
287 	__SETREG(4, count); \
288 	asm("tw 31,r30,r30"); \
289 } while (0)
290 #define OSDebugMessage(message) OSDebugMessageBytes(message, 0)
291 #define OSDebugStrIfConnected(message) do { if (OSIsDebuggerPresent()) OSDebugStr(message); } while (0)
292 #else
293 extern void OSDebug(void);
294 extern void OSDebugStr(const char *message);
295 extern void OSDebugMessage(const char *message);
296 extern void OSDebugMessageBytes(const char *message, unsigned int count);
297 extern void OSDebugStrIfConnected(const char *message);
298 #endif
299 
300 /*---------------------------------------------------------------------------*
301     Get infomation APIs
302  *---------------------------------------------------------------------------*/
303 
304 const char* OSGetAppGamename(void);
305 const u8    OSGetAppType(void);
306 #define  OS_APP_TYPE_WC24             0x20
307 #define  OS_APP_TYPE_IPL              0x40
308 #define  OS_APP_TYPE_DVD              0x80
309 #define  OS_APP_TYPE_NAND             0x81
310 
311 #ifdef __cplusplus
312 }
313 #endif
314 
315 /*---------------------------------------------------------------------------*
316     Advanced OS APIs
317  *---------------------------------------------------------------------------*/
318 
319 #include <cafe/os/OSCore.h>      // Processor Core routines and defines
320 #include <cafe/os/OSSync.h>      // Synchronization routines and defines
321 #include <cafe/os/OSAlarm.h>     // Alarm routines and defines
322 #include <cafe/os/OSAtomic.h>    // Atomic operation
323 #include <cafe/os/OSCache.h>     // Cache routines and defines
324 #include <cafe/os/OSContext.h>   // Context structures and defines
325 #include <cafe/os/OSDeviceThread.h> // Threads for device drivers and user callback functions
326 #include <cafe/os/OSError.h>     // Error handler routines and defines
327 #include <cafe/os/OSFastCast.h>  // Fast float/int conversion
328 #include <cafe/os/OSMessage.h>   // Message routines and defines
329 #include <cafe/os/OSMutex.h>     // Mutex routines and defines
330 #include <cafe/os/OSFastMutex.h> // Fast Mutex routines which are not thread cancel points
331 #include <cafe/os/OSSemaphore.h> // Semaphore routines and defines
332 #include <cafe/os/OSEvent.h>
333 #include <cafe/os/OSSystemInfo.h>
334 #include <cafe/os/OSThread.h>    // Thread routines and defines
335 #include <cafe/os/OSReset.h>     // Reset APIs
336 #include <cafe/os/OSDynLoad.h>
337 #include <cafe/os/OSFunctionType.h>
338 #include <cafe/os/OSPerformanceMonitor.h>
339 #include <cafe/os/OSLaunch.h>
340 #include <cafe/os/OSCoroutine.h>
341 #include <cafe/os/OSMemory.h>
342 #include <cafe/os/OSMemmap.h>    // Memory mapping APIs
343 #include <cafe/os/OSPlatform.h>  // Platform information
344 #include <cafe/os/OSSysHealth.h>
345 
346 #define WEAK_SYMBOL
347 
348 #endif // _ASSEMBLER
349 #endif  // __OS_H__
350 
351