-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathsifrpc.c
699 lines (556 loc) · 16 KB
/
sifrpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
/*
# _____ ___ ____ ___ ____
# ____| | ____| | | |____|
# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
#-----------------------------------------------------------------------
# (C)2001, Gustavo Scotti ([email protected])
# (c) 2003 Marcus R. Brown ([email protected])
# Licenced under Academic Free License version 2.0
# Review ps2sdk README & LICENSE files for further details.
*/
/**
* @file
* EE SIF RPC commands
* MRB: This file now contains the SIF routines included
* with libpsware. Bug reports welcome.
*/
#include <tamtypes.h>
#include <ps2lib_err.h>
#include <kernel.h>
#include <sifcmd.h>
#include <sifrpc.h>
#define RPC_PACKET_SIZE 64
/** Set if the packet has been allocated */
#define PACKET_F_ALLOC 0x01
static inline void rpc_packet_free(void *packet)
{
SifRpcRendPkt_t *rendpkt = (SifRpcRendPkt_t *)packet;
rendpkt->rpc_id = 0;
rendpkt->rec_id &= (~PACKET_F_ALLOC);
}
struct rpc_data
{
int pid;
void *pkt_table;
int pkt_table_len;
int unused1;
int unused2;
u8 *rdata_table;
int rdata_table_len;
u8 *client_table;
int client_table_len;
int rdata_table_idx;
void *active_queue;
} __attribute__((aligned(64)));
extern int _iop_reboot_count;
extern struct rpc_data _sif_rpc_data;
void *_rpc_get_packet(struct rpc_data *rpc_data);
void *_rpc_get_fpacket(struct rpc_data *rpc_data);
#ifdef F__rpc_get_packet
void *_rpc_get_packet(struct rpc_data *rpc_data)
{
SifRpcPktHeader_t *packet;
int len;
DI();
len = rpc_data->pkt_table_len;
if (len > 0) {
int pid, rid;
packet = (SifRpcPktHeader_t *)rpc_data->pkt_table;
for (rid = 0; rid < len; rid++, packet = (SifRpcPktHeader_t *)(((unsigned char *)packet) + RPC_PACKET_SIZE)) {
if (!(packet->rec_id & PACKET_F_ALLOC))
break;
}
if (rid == len) {
EI();
return NULL;
}
pid = rpc_data->pid;
if (pid) {
rpc_data->pid = ++pid;
} else {
rpc_data->pid = pid + 2;
pid = 1;
}
packet->rec_id = (rid << 16) | 0x04 | PACKET_F_ALLOC;
packet->rpc_id = pid;
packet->pkt_addr = packet;
EI();
return packet;
}
EI();
return NULL;
}
#endif
#ifdef F__rpc_get_fpacket
void *_rpc_get_fpacket(struct rpc_data *rpc_data)
{
int index;
index = rpc_data->rdata_table_idx % rpc_data->rdata_table_len;
rpc_data->rdata_table_idx = index + 1;
return (void *)(rpc_data->rdata_table + (index * RPC_PACKET_SIZE));
}
#endif
#ifdef F_sceSifBindRpc
int sceSifBindRpc(SifRpcClientData_t *cd, int sid, int mode)
{
ee_sema_t sema;
SifRpcBindPkt_t *bind;
bind = (SifRpcBindPkt_t *)_rpc_get_packet(&_sif_rpc_data);
if (!bind)
return -E_SIF_PKT_ALLOC;
cd->command = 0;
cd->server = NULL;
cd->hdr.pkt_addr = bind;
cd->hdr.rpc_id = bind->rpc_id;
cd->hdr.sema_id = -1;
bind->sid = sid;
bind->pkt_addr = bind;
bind->cd = cd;
if (mode & SIF_RPC_M_NOWAIT) {
if (!sceSifSendCmd(SIF_CMD_RPC_BIND, bind, RPC_PACKET_SIZE, NULL, NULL, 0)) {
rpc_packet_free(bind);
return -E_SIF_PKT_SEND;
}
return 0;
}
sema.max_count = 1;
sema.init_count = 0;
cd->hdr.sema_id = CreateSema(&sema);
if (cd->hdr.sema_id < 0) {
rpc_packet_free(bind);
return -E_LIB_SEMA_CREATE;
}
if (!sceSifSendCmd(SIF_CMD_RPC_BIND, bind, RPC_PACKET_SIZE, NULL, NULL, 0)) {
rpc_packet_free(bind);
DeleteSema(cd->hdr.sema_id);
return -E_SIF_PKT_SEND;
}
WaitSema(cd->hdr.sema_id);
DeleteSema(cd->hdr.sema_id);
return 0;
}
#endif
#ifdef F_sceSifCallRpc
int sceSifCallRpc(SifRpcClientData_t *cd, int rpc_number, int mode, void *sendbuf,
int ssize, void *recvbuf, int rsize, SifRpcEndFunc_t end_function, void *end_param)
{
ee_sema_t sema;
SifRpcCallPkt_t *call;
call = (SifRpcCallPkt_t *)_rpc_get_packet(&_sif_rpc_data);
if (!call)
return -E_SIF_PKT_ALLOC;
cd->hdr.pkt_addr = call;
cd->hdr.rpc_id = call->rpc_id;
cd->hdr.sema_id = -1;
cd->end_function = end_function;
cd->end_param = end_param;
call->rpc_number = rpc_number;
call->send_size = ssize;
call->recvbuf = recvbuf;
call->recv_size = rsize;
call->rmode = 1;
call->pkt_addr = call;
call->cd = cd;
call->sd = cd->server;
if (!(mode & SIF_RPC_M_NOWBDC)) {
if (ssize > 0)
sceSifWriteBackDCache(sendbuf, ssize);
if (rsize > 0)
sceSifWriteBackDCache(recvbuf, rsize);
}
if (mode & SIF_RPC_M_NOWAIT) {
if (!end_function)
call->rmode = 0;
if (!sceSifSendCmd(SIF_CMD_RPC_CALL, call, RPC_PACKET_SIZE, sendbuf, cd->buf, ssize)) {
rpc_packet_free(call);
return -E_SIF_PKT_SEND;
}
return 0;
}
sema.max_count = 1;
sema.init_count = 0;
cd->hdr.sema_id = CreateSema(&sema);
if (cd->hdr.sema_id < 0) {
rpc_packet_free(call);
return -E_LIB_SEMA_CREATE;
}
if (!sceSifSendCmd(SIF_CMD_RPC_CALL, call, RPC_PACKET_SIZE, sendbuf, cd->buf, ssize)) {
rpc_packet_free(call);
DeleteSema(cd->hdr.sema_id);
return -E_SIF_PKT_SEND;
}
WaitSema(cd->hdr.sema_id);
DeleteSema(cd->hdr.sema_id);
return 0;
}
#endif
#ifdef F_sceSifGetOtherData
int sceSifGetOtherData(SifRpcReceiveData_t *rd, void *src, void *dest, int size, int mode)
{
ee_sema_t sema;
SifRpcOtherDataPkt_t *other;
other = (SifRpcOtherDataPkt_t *)_rpc_get_packet(&_sif_rpc_data);
if (!other)
return -E_SIF_PKT_ALLOC;
rd->hdr.pkt_addr = other;
rd->hdr.rpc_id = other->rpc_id;
rd->hdr.sema_id = -1;
other->src = src;
other->dest = dest;
other->size = size;
other->recvbuf = rd;
if (mode & SIF_RPC_M_NOWAIT) {
if (!sceSifSendCmd(SIF_CMD_RPC_RDATA, other, RPC_PACKET_SIZE, NULL, NULL, 0)) {
rpc_packet_free(other);
return -E_SIF_PKT_SEND;
}
return 0;
}
sema.max_count = 1;
sema.init_count = 0;
rd->hdr.sema_id = CreateSema(&sema);
if (rd->hdr.sema_id < 0) {
rpc_packet_free(other);
return -E_LIB_SEMA_CREATE;
}
if (!sceSifSendCmd(SIF_CMD_RPC_RDATA, other, RPC_PACKET_SIZE, NULL, NULL, 0)) {
rpc_packet_free(other);
DeleteSema(rd->hdr.sema_id);
return -E_SIF_PKT_SEND;
}
WaitSema(rd->hdr.sema_id);
DeleteSema(rd->hdr.sema_id);
return 0;
}
#endif
#ifdef F_SifRpcMain
/* The packets sent on EE RPC requests are allocated from this table. */
static u8 pkt_table[2048] __attribute__((aligned(64)));
/* A ring buffer used to allocate packets sent on IOP requests. */
static u8 rdata_table[2048] __attribute__((aligned(64)));
static u8 client_table[2048] __attribute__((aligned(64)));
struct rpc_data _sif_rpc_data = {
pid : 1,
pkt_table : pkt_table,
pkt_table_len : sizeof(pkt_table) / RPC_PACKET_SIZE,
rdata_table : rdata_table,
rdata_table_len : sizeof(rdata_table) / RPC_PACKET_SIZE,
client_table : client_table,
client_table_len : sizeof(client_table) / RPC_PACKET_SIZE,
rdata_table_idx : 0
};
static int init = 0;
/* Command 0x80000008 */
static void _request_end(SifRpcRendPkt_t *request, void *data)
{
SifRpcClientData_t *cd = request->cd;
(void)data;
if (request->cid == SIF_CMD_RPC_CALL) {
if (cd->end_function)
cd->end_function(cd->end_param);
} else if (request->cid == SIF_CMD_RPC_BIND) {
cd->server = request->sd;
cd->buf = request->buf;
cd->cbuf = request->cbuf;
}
if (cd->hdr.sema_id >= 0)
iSignalSema(cd->hdr.sema_id);
rpc_packet_free(cd->hdr.pkt_addr);
cd->hdr.pkt_addr = NULL;
}
static void *search_svdata(u32 sid, struct rpc_data *rpc_data)
{
SifRpcServerData_t *sd;
SifRpcDataQueue_t *queue = rpc_data->active_queue;
if (!queue)
return NULL;
while (queue) {
sd = queue->link;
while (sd) {
if ((u32)(sd->sid) == sid)
return sd;
sd = sd->link;
}
queue = queue->next;
}
return NULL;
}
/* Command 0x80000009 */
static void _request_bind(SifRpcBindPkt_t *bind, void *data)
{
SifRpcRendPkt_t *rend;
SifRpcServerData_t *sd;
rend = _rpc_get_fpacket(data);
rend->pkt_addr = bind->pkt_addr;
rend->cd = bind->cd;
rend->cid = SIF_CMD_RPC_BIND;
sd = search_svdata(bind->sid, data);
if (!sd) {
rend->sd = NULL;
rend->buf = NULL;
rend->cbuf = NULL;
} else {
rend->sd = sd;
rend->buf = sd->buf;
rend->cbuf = sd->cbuf;
}
isceSifSendCmd(SIF_CMD_RPC_END, rend, RPC_PACKET_SIZE, NULL, NULL, 0);
}
/* Command 0x8000000a */
static void _request_call(SifRpcCallPkt_t *request, void *data)
{
SifRpcServerData_t *sd = request->sd;
SifRpcDataQueue_t *base = sd->base;
(void)data;
if (base->start)
base->end->next = sd;
else
base->start = sd;
base->end = sd;
sd->pkt_addr = request->pkt_addr;
sd->client = request->cd;
sd->rpc_number = request->rpc_number;
sd->size = request->send_size;
sd->recvbuf = request->recvbuf;
sd->rsize = request->recv_size;
sd->rmode = request->rmode;
sd->rid = request->rec_id;
if (base->thread_id < 0 || base->active != 0)
return;
iWakeupThread(base->thread_id);
}
/* Command 0x8000000c */
static void _request_rdata(SifRpcOtherDataPkt_t *rdata, void *data)
{
SifRpcRendPkt_t *rend;
rend = (SifRpcRendPkt_t *)_rpc_get_fpacket(data);
rend->pkt_addr = rdata->pkt_addr;
rend->cd = (SifRpcClientData_t *)rdata->recvbuf;
rend->cid = SIF_CMD_RPC_RDATA;
isceSifSendCmd(SIF_CMD_RPC_END, rend, RPC_PACKET_SIZE, rdata->src, rdata->dest, rdata->size);
}
void sceSifInitRpc(int mode)
{
u32 *cmdp;
(void)mode;
static int _rb_count = 0;
if (_rb_count != _iop_reboot_count) {
_rb_count = _iop_reboot_count;
sceSifExitCmd();
init = 0;
}
if (init)
return;
init = 1;
sceSifInitCmd();
DI();
_sif_rpc_data.pkt_table = UNCACHED_SEG(_sif_rpc_data.pkt_table);
_sif_rpc_data.rdata_table = UNCACHED_SEG(_sif_rpc_data.rdata_table);
_sif_rpc_data.client_table = UNCACHED_SEG(_sif_rpc_data.client_table);
_sif_rpc_data.rdata_table_idx = 0;
struct rpc_data *rpc_data = (struct rpc_data *)(&_sif_rpc_data);
int len = rpc_data->pkt_table_len;
if (len > 0) {
int rid;
SifRpcPktHeader_t *packet = (SifRpcPktHeader_t *)rpc_data->pkt_table;
for (rid = 0; rid < len; rid++, packet = (SifRpcPktHeader_t *)(((unsigned char *)packet) + RPC_PACKET_SIZE)) {
rpc_packet_free(packet);
}
}
sceSifAddCmdHandler(SIF_CMD_RPC_END, (void *)_request_end, &_sif_rpc_data);
sceSifAddCmdHandler(SIF_CMD_RPC_BIND, (void *)_request_bind, &_sif_rpc_data);
sceSifAddCmdHandler(SIF_CMD_RPC_CALL, (void *)_request_call, &_sif_rpc_data);
sceSifAddCmdHandler(SIF_CMD_RPC_RDATA, (void *)_request_rdata, &_sif_rpc_data);
EI();
if (sceSifGetReg(SIF_SYSREG_RPCINIT))
return;
cmdp = (u32 *)&pkt_table[64];
cmdp[3] = 1;
sceSifSendCmd(SIF_CMD_INIT_CMD, cmdp, 16, NULL, NULL, 0);
while (!sceSifGetSreg(SIF_SREG_RPCINIT))
;
sceSifSetReg(SIF_SYSREG_RPCINIT, 1);
}
void sceSifExitRpc(void)
{
sceSifExitCmd();
init = 0;
}
#endif
#ifdef F_sceSifRegisterRpc
void sceSifRegisterRpc(SifRpcServerData_t *sd, int sid, SifRpcFunc_t func, void *buf,
SifRpcFunc_t cfunc, void *cbuf, SifRpcDataQueue_t *qd)
{
SifRpcServerData_t *server;
DI();
sd->link = NULL;
sd->next = NULL;
sd->sid = sid;
sd->func = func;
sd->buf = buf;
sd->cfunc = cfunc;
sd->cbuf = cbuf;
sd->base = qd;
if (!(server = qd->link)) {
qd->link = sd;
} else {
while (server->link != NULL) {
server = server->link;
}
server->link = sd;
}
EI();
}
#endif
#ifdef F_sceSifRemoveRpc
SifRpcServerData_t *sceSifRemoveRpc(SifRpcServerData_t *sd, SifRpcDataQueue_t *qd)
{
SifRpcServerData_t *server;
DI();
if ((server = qd->link) == sd) {
qd->link = server->link;
} else {
while (server != NULL) {
if (server->link == sd) {
server->link = sd->link;
break;
}
server = server->link;
}
}
EI();
return server;
}
#endif
#ifdef F_sceSifSetRpcQueue
void sceSifSetRpcQueue(SifRpcDataQueue_t *qd, int thread_id)
{
SifRpcDataQueue_t *queue = NULL;
DI();
qd->thread_id = thread_id;
qd->active = 0;
qd->link = NULL;
qd->start = NULL;
qd->end = NULL;
qd->next = NULL;
if (_sif_rpc_data.active_queue == NULL) {
_sif_rpc_data.active_queue = qd;
} else {
queue = _sif_rpc_data.active_queue;
while (queue->next != NULL)
queue = queue->next;
queue->next = qd;
}
EI();
}
#endif
#ifdef F_sceSifRemoveRpcQueue
SifRpcDataQueue_t *sceSifRemoveRpcQueue(SifRpcDataQueue_t *qd)
{
SifRpcDataQueue_t *queue;
DI();
if ((queue = _sif_rpc_data.active_queue) == qd) {
_sif_rpc_data.active_queue = queue->next;
} else {
while (queue != NULL) {
if (queue->next == qd) {
queue->next = qd->next;
break;
}
queue = queue->next;
}
}
EI();
return queue;
}
#endif
#ifdef F_sceSifGetNextRequest
SifRpcServerData_t *sceSifGetNextRequest(SifRpcDataQueue_t *qd)
{
SifRpcServerData_t *sd;
DI();
sd = qd->start;
if (sd != NULL) {
qd->active = 1;
qd->start = sd->next;
} else {
qd->active = 0;
}
EI();
return sd;
}
#endif
#ifdef F_sceSifExecRequest
static void *_rpc_get_fpacket2(struct rpc_data *rpc_data, int rid)
{
if (rid < 0 || rid < rpc_data->client_table_len)
return _rpc_get_fpacket(rpc_data);
else
return rpc_data->client_table + (rid * RPC_PACKET_SIZE);
}
void sceSifExecRequest(SifRpcServerData_t *sd)
{
SifDmaTransfer_t dmat;
SifRpcRendPkt_t *rend;
void *rec = NULL;
rec = sd->func(sd->rpc_number, sd->buf, sd->size);
if (sd->size)
sceSifWriteBackDCache(sd->buf, sd->size);
if (sd->rsize)
sceSifWriteBackDCache(rec, sd->rsize);
DI();
if (sd->rid & 4)
rend = (SifRpcRendPkt_t *)
_rpc_get_fpacket2(&_sif_rpc_data, (sd->rid >> 16) & 0xffff);
else
rend = (SifRpcRendPkt_t *)
_rpc_get_fpacket(&_sif_rpc_data);
EI();
rend->cd = sd->client;
rend->cid = SIF_CMD_RPC_CALL;
if (sd->rmode) {
if (!sceSifSendCmd(SIF_CMD_RPC_END, rend, RPC_PACKET_SIZE, rec, sd->recvbuf,
sd->rsize))
return;
}
rend->rpc_id = 0;
rend->rec_id = 0;
if (sd->rsize) {
dmat.src = rec;
dmat.dest = sd->recvbuf;
dmat.size = sd->rsize;
dmat.attr = 0;
} else {
dmat.src = rend;
dmat.dest = sd->pkt_addr;
dmat.size = 64;
dmat.attr = 0;
}
while (!sceSifSetDma(&dmat, 1))
nopdelay();
}
#endif
#ifdef F_sceSifRpcLoop
void sceSifRpcLoop(SifRpcDataQueue_t *qd)
{
while (1) {
SifRpcServerData_t *sd;
while ((sd = sceSifGetNextRequest(qd)))
sceSifExecRequest(sd);
SleepThread();
}
}
#endif
#ifdef F_sceSifCheckStatRpc
int sceSifCheckStatRpc(SifRpcClientData_t *cd)
{
SifRpcPktHeader_t *packet = (SifRpcPktHeader_t *)cd->hdr.pkt_addr;
if (!packet)
return 0;
if (cd->hdr.rpc_id != (u32)(packet->rpc_id))
return 0;
if (!(packet->rec_id & PACKET_F_ALLOC))
return 0;
return 1;
}
#endif