LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
xenon_syscalls.c
Go to the documentation of this file.
1#include <_ansi.h>
2#include <_syslist.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6#include <errno.h>
7#include <sys/iosupport.h>
8#include <sys/dirent.h>
9#include <sys/errno.h>
10#include <sys/time.h>
11#include <time/time.h>
12
13#include <assert.h>
14#include <debug.h>
15
16#include <ppc/atomic.h>
17#include <ppc/register.h>
18
20#include <xenon_smc/xenon_smc.h>
21
22#define XELL_FOOTER_OFFSET (256*1024-16)
23#define XELL_FOOTER_LENGTH 16
24#define XELL_FOOTER "xxxxxxxxxxxxxxxx"
25
26extern unsigned char heap_begin;
27unsigned char *heap_ptr;
28
29// prototype
30static void xenon_exit(int status);
31static void *xenon_sbrk(struct _reent *ptr, ptrdiff_t incr);
32static int xenon_gettimeofday(struct _reent *ptr, struct timeval *tp, struct timezone *tz);
33static void xenon_malloc_unlock(struct _reent *_r);
34static void xenon_malloc_lock(struct _reent *_r);
35
36ssize_t vfs_console_write(struct _reent *r, int fd, const char *src, size_t len);
37
38extern void return_to_xell(unsigned int nand_addr, unsigned int phy_loading_addr);
39extern void enet_quiesce();
40extern void usb_shutdown(void);
41
42//---------------------------------------------------------------------------------
44 //---------------------------------------------------------------------------------
45 xenon_sbrk, // sbrk
46 NULL, // lock_init
47 NULL, // lock_close
48 NULL, // lock_release
49 NULL, // lock_acquire
50 xenon_malloc_lock, // malloc_lock
51 xenon_malloc_unlock, // malloc_unlock
52 xenon_exit, // exit
53 xenon_gettimeofday // gettod_r
54};
55
56
57// 22 nov 2005
58#define RTC_BASE 1005782400UL//1132614024UL
59
60static int xenon_gettimeofday(struct _reent *ptr, struct timeval *tp, struct timezone *tz) {
61 unsigned char msg[16] = {0x04};
62 union{
63 uint8_t u8[8];
64 uint64_t t;
65 } time;
66 time.t = 0;
67
70
71 time.u8[3] = msg[5];
72 time.u8[4] = msg[4];
73 time.u8[5] = msg[3];
74 time.u8[6] = msg[2];
75 time.u8[7] = msg[1];
76
77 tp->tv_sec = (time.t / 1000) + RTC_BASE;
78 tp->tv_usec = (time.t % 1000) * 1000;
79
80 return 0;
81}
82
83static void *xenon_sbrk(struct _reent *ptr, ptrdiff_t incr) {
84 unsigned char *res;
85 if (!heap_ptr)
87 res = heap_ptr;
88 heap_ptr += incr;
89 return res;
90}
91
93 // some drivers require a shutdown
97}
98
99void try_return_to_xell(unsigned int nand_addr, unsigned int phy_loading_addr) {
100 if (!memcmp((void*) (nand_addr + XELL_FOOTER_OFFSET), XELL_FOOTER, XELL_FOOTER_LENGTH))
101 return_to_xell(nand_addr, phy_loading_addr);
102}
103
104static void xenon_exit(int status) {
105 char s[256];
106 int i, stuck = 0;
107
108 // all threads are down remove syscall for lock and unlock
111
112 // network is down too
113 //network_stdout_hook = NULL;
114
115 sprintf(s, "[Exit] with code %d\n", status);
116 vfs_console_write(NULL, 0, s, strlen(s));
117
118 for (i = 0; i < 6; ++i) {
120 sprintf(s, "Thread %d is still running !\n", i);
121 vfs_console_write(NULL, 0, s, strlen(s));
122 stuck = 1;
123 }
124 }
125
127
128 if (stuck) {
129 sprintf(s, "Can't reload Xell, looping...");
130 vfs_console_write(NULL, 0, s, strlen(s));
131 } else {
132 sprintf(s, "Reloading Xell...");
133 vfs_console_write(NULL, 0, s, strlen(s));
135
136 try_return_to_xell(0xc8070000, 0x1c000000); // xell-gggggg (ggboot)
137 try_return_to_xell(0xc8095060, 0x1c040000); // xell-2f (freeboot)
138 try_return_to_xell(0xc8100000, 0x1c000000); // xell-1f, xell-ggggggg
139
140 sprintf(s, "Failed to load Xell, rebooting in 10 seconds...");
141 vfs_console_write(NULL, 0, s, strlen(s));
142 delay(10);
144 }
145
146 for (;;);
147}
148
149
150static unsigned int __attribute__((aligned(128))) malloc_spinlock = 0;
151static unsigned int __attribute__((aligned(128))) safety_spinlock = 0;
152static volatile int lockcount = 0;
153static volatile unsigned int lockowner = -1;
154
155static void xenon_malloc_lock(struct _reent *_r) {
156 lock(&safety_spinlock);
157
158 int llc=lockcount;
159 unsigned int llo=lockowner;
160 unsigned int pir_=mfspr(pir);
161
162 assert(llc >= 0);
163
164 unlock(&safety_spinlock);
165
166 if (llc == 0 || llo != pir_)
167 {
168 lock(&malloc_spinlock);
169
170 lock(&safety_spinlock);
171
172 ++lockcount;
173 lockowner = pir_;
174
175 unlock(&safety_spinlock);
176 }
177 else
178 {
179 lock(&safety_spinlock);
180
181 ++lockcount;
182
183 unlock(&safety_spinlock);
184 }
185}
186
187static void xenon_malloc_unlock(struct _reent *_r) {
188 lock(&safety_spinlock);
189
190 int llc=lockcount;
191 unsigned int llo=lockowner;
192
193 --lockcount;
194
195 assert(llc > 0);
196 assert(llo == mfspr(pir));
197
198 if (llc == 1)
199 {
200 unlock(&malloc_spinlock);
201 lockowner = -1;
202 }
203
204 unlock(&safety_spinlock);
205}
void lock(unsigned int *lock)
void unlock(unsigned int *lock)
void data_breakpoint(void *address, int on_read, int on_write)
Definition: debug.c:56
#define NULL
Definition: def.h:47
u32 status
Definition: ehci_defs.h:15
u32 ptr
Definition: iso9660.c:536
u64 uint64_t
Definition: libfdt_env.h:12
u8 uint8_t
Definition: libfdt_env.h:9
unsigned int __mf_uintptr_t __attribute__((__mode__(__pointer__)))
Definition: mf-runtime.h:34
#define mfspr(rn)
Definition: register.h:16
void(* malloc_lock)(struct _reent *ptr)
Definition: iosupport.h:74
void(* malloc_unlock)(struct _reent *ptr)
Definition: iosupport.h:75
void delay(int u)
Definition: time.c:22
int xenon_is_thread_task_running(int thread)
Definition: xenon_power.c:217
void xenon_set_single_thread_mode()
Definition: xenon_power.c:276
void xenon_smc_send_message(const unsigned char *msg)
Definition: xenon_smc.c:20
void xenon_smc_power_reboot(void)
Definition: xenon_smc.c:274
int xenon_smc_receive_response(unsigned char *msg)
Definition: xenon_smc.c:98
void return_to_xell(unsigned int nand_addr, unsigned int phy_loading_addr)
unsigned char * heap_ptr
void enet_quiesce()
Definition: enet.c:419
#define RTC_BASE
#define XELL_FOOTER
__syscalls_t __syscalls
#define XELL_FOOTER_LENGTH
void try_return_to_xell(unsigned int nand_addr, unsigned int phy_loading_addr)
unsigned char heap_begin
ssize_t vfs_console_write(struct _reent *r, int fd, const char *src, size_t len)
Definition: newlib.c:25
void usb_shutdown(void)
Definition: usbmain.c:280
void shutdown_drivers()
#define XELL_FOOTER_OFFSET
#define pir
Definition: xenonsprs.h:59
uint8_t u8
8bit unsigned integer
Definition: xetypes.h:12