-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpsp-proxy.c
More file actions
684 lines (533 loc) · 20.9 KB
/
psp-proxy.c
File metadata and controls
684 lines (533 loc) · 20.9 KB
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
/** @file
* PSP proxy library to interface with the hardware of the PSP
*/
/*
* Copyright (C) 2019-2020 Alexander Eichner <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _DEFAULT_SOURCE
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include "psp-proxy-provider.h"
#include "psp-stub-pdu.h"
/**
* A free chunk of scratch space memory.
*/
typedef struct PSPSCRATCHCHUNKFREE
{
/** Pointer to the next free chunk or NULL if end of list. */
struct PSPSCRATCHCHUNKFREE *pNext;
/** Pointer to the previous free chunk or NULL if head of list. */
struct PSPSCRATCHCHUNKFREE *pPrev;
/** Start address of the free chunk. */
PSPADDR PspAddrStart;
/** Size of the chunk. */
size_t cbChunk;
} PSPSCRATCHCHUNKFREE;
/** Pointer to a free scratch space chunk. */
typedef PSPSCRATCHCHUNKFREE *PPSPSCRATCHCHUNKFREE;
/**
* Internal PSP proxy context.
*/
typedef struct PSPPROXYCTXINT
{
/** The current CCD ID set. */
uint32_t idCcd;
/** I/O interface. */
PCPSPPROXYIOIF pIoIf;
/** Opaque user data to pass to the I/O interface callbacks. */
void *pvUser;
/** Flag whether the scratch space manager was initialized. */
int fScratchSpaceMgrInit;
/** List of free scratch space blocks, sorted by PSP address (lowest is head). */
PPSPSCRATCHCHUNKFREE pScratchFreeHead;
/** The provider used. */
PCPSPPROXYPROV pProv;
/** The stub PDU context. */
PSPSTUBPDUCTX hPduCtx;
/** The provider specific context data, variable in size. */
uint8_t abProvCtx[1];
} PSPPROXYCTXINT;
/** Pointer to an internal PSP proxy context. */
typedef PSPPROXYCTXINT *PPSPPROXYCTXINT;
//extern const PSPPROXYPROV g_PspProxyProvSev;
extern const PSPPROXYPROV g_PspProxyProvSerial;
extern const PSPPROXYPROV g_PspProxyProvTcp;
//extern const PSPPROXYPROV g_PspProxyProvEm100Tcp;
/**
* Array of known PSP proxy providers.
*/
static PCPSPPROXYPROV g_apPspProxyProv[] =
{
// &g_PspProxyProvSev,
&g_PspProxyProvSerial,
&g_PspProxyProvTcp,
// &g_PspProxyProvEm100Tcp,
NULL
};
/**
* Initializes the scratch space manager.
*
* @returns Status code.
* @param pThis The context instance.
*/
static int pspProxyCtxScratchSpaceMgrInit(PPSPPROXYCTXINT pThis)
{
PSPADDR PspAddrScratchStart = 0;
size_t cbScratch = 0;
int rc = pspStubPduCtxQueryInfo(pThis->hPduCtx, pThis->idCcd, &PspAddrScratchStart, &cbScratch);
if (!rc)
{
/* Set up the first chunk covering the whole scratch space area. */
PPSPSCRATCHCHUNKFREE pFree = (PPSPSCRATCHCHUNKFREE)malloc(sizeof(*pFree));
if (pFree)
{
pFree->pNext = NULL;
pFree->pPrev = NULL;
pFree->PspAddrStart = PspAddrScratchStart;
pFree->cbChunk = cbScratch;
pThis->pScratchFreeHead = pFree;
pThis->fScratchSpaceMgrInit = 1;
}
else
rc = -1;
}
return rc;
}
/**
* Finds the appropriate proxy provider from the given device URI.
*
* @returns Pointer to the matching provider or NULL if none was found.
* @param pszDevice The device URI to match.
* @param ppszDevRem Where to store the pointer to remainder of the device string passed to the provider
* during initialization.
*/
static PCPSPPROXYPROV pspProxyCtxProvFind(const char *pszDevice, const char **ppszDevRem)
{
size_t cchDevice = strlen(pszDevice);
const char *pszSep = strchr(pszDevice, ':');
if ( pszSep
&& cchDevice - (pszSep - pszDevice) >= 3
&& pszSep[1] == '/'
&& pszSep[2] == '/')
{
size_t cchProv = pszSep - pszDevice;
PCPSPPROXYPROV *ppProv = &g_apPspProxyProv[0];
while (*ppProv)
{
if (!strncmp(pszDevice, (*ppProv)->pszId, cchProv))
{
*ppszDevRem = pszSep + 3;
return *ppProv; /* Found */
}
ppProv++;
}
}
return NULL;
}
int PSPProxyCtxCreate(PPSPPROXYCTX phCtx, const char *pszDevice, PCPSPPROXYIOIF pIoIf,
void *pvUser)
{
int rc = 0;
const char *pszDevRem = NULL;
PCPSPPROXYPROV pProv = pspProxyCtxProvFind(pszDevice, &pszDevRem);
if (pProv)
{
PPSPPROXYCTXINT pThis = (PPSPPROXYCTXINT)calloc(1, sizeof(*pThis) + pProv->cbCtx);
if (pThis != NULL)
{
pThis->idCcd = 0;
pThis->pIoIf = pIoIf;
pThis->pvUser = pvUser;
pThis->fScratchSpaceMgrInit = 0;
pThis->pProv = pProv;
rc = pProv->pfnCtxInit((PSPPROXYPROVCTX)&pThis->abProvCtx[0], pszDevRem);
if (!rc)
{
/* Create the PDU context. */
rc = pspStubPduCtxCreate(&pThis->hPduCtx, pProv, (PSPPROXYPROVCTX)&pThis->abProvCtx[0],
pIoIf, pThis, pvUser);
if (!rc)
{
rc = pspStubPduCtxConnect(pThis->hPduCtx, 10 * 1000);
if (!rc)
{
*phCtx = pThis;
return 0;
}
pspStubPduCtxDestroy(pThis->hPduCtx);
}
pThis->pProv->pfnCtxDestroy((PSPPROXYPROVCTX)&pThis->abProvCtx[0]);
}
free(pThis);
}
else
rc = -1;
}
else
rc = -1;
return rc;
}
void PSPProxyCtxDestroy(PSPPROXYCTX hCtx)
{
PPSPPROXYCTXINT pThis = hCtx;
pspStubPduCtxDestroy(pThis->hPduCtx);
pThis->pProv->pfnCtxDestroy((PSPPROXYPROVCTX)&pThis->abProvCtx[0]);
free(pThis);
}
int PSPProxyCtxPspCcdSet(PSPPROXYCTX hCtx, uint32_t idCcd)
{
PPSPPROXYCTXINT pThis = hCtx;
/** @todo Check that the ID is in range. */
pThis->idCcd = idCcd;
return 0;
}
int PSPProxyCtxQueryLastReqRc(PSPPROXYCTX hCtx, PSPSTS *pReqRcLast)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxQueryLastReqRc(pThis->hPduCtx, pReqRcLast);
}
int PSPProxyCtxPspSmnRead(PSPPROXYCTX hCtx, uint32_t idCcdTgt, SMNADDR uSmnAddr, uint32_t cbVal, void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspSmnRead(pThis->hPduCtx, pThis->idCcd, idCcdTgt, uSmnAddr, cbVal, pvVal);
}
int PSPProxyCtxPspSmnWrite(PSPPROXYCTX hCtx, uint32_t idCcdTgt, SMNADDR uSmnAddr, uint32_t cbVal, const void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspSmnWrite(pThis->hPduCtx, pThis->idCcd, idCcdTgt, uSmnAddr, cbVal, pvVal);
}
int PSPProxyCtxPspMemRead(PSPPROXYCTX hCtx, PSPADDR uPspAddr, void *pvBuf, uint32_t cbRead)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspMemRead(pThis->hPduCtx, pThis->idCcd, uPspAddr, pvBuf, cbRead);
}
int PSPProxyCtxPspMemWrite(PSPPROXYCTX hCtx, PSPADDR uPspAddr, const void *pvBuf, uint32_t cbWrite)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspMemWrite(pThis->hPduCtx, pThis->idCcd, uPspAddr, pvBuf, cbWrite);
}
int PSPProxyCtxPspMmioRead(PSPPROXYCTX hCtx, PSPADDR uPspAddr, uint32_t cbVal, void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspMmioRead(pThis->hPduCtx, pThis->idCcd, uPspAddr, pvVal, cbVal);
}
int PSPProxyCtxPspMmioWrite(PSPPROXYCTX hCtx, PSPADDR uPspAddr, uint32_t cbVal, const void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspMmioWrite(pThis->hPduCtx, pThis->idCcd, uPspAddr, pvVal, cbVal);
}
int PSPProxyCtxPspX86MemRead(PSPPROXYCTX hCtx, X86PADDR PhysX86Addr, void *pvBuf, uint32_t cbRead)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspX86MemRead(pThis->hPduCtx, pThis->idCcd, PhysX86Addr, pvBuf, cbRead);
}
int PSPProxyCtxPspX86MemWrite(PSPPROXYCTX hCtx, X86PADDR PhysX86Addr, const void *pvBuf, uint32_t cbWrite)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspX86MemWrite(pThis->hPduCtx, pThis->idCcd, PhysX86Addr, pvBuf, cbWrite);
}
int PSPProxyCtxPspX86MmioRead(PSPPROXYCTX hCtx, X86PADDR PhysX86Addr, uint32_t cbVal, void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspX86MmioRead(pThis->hPduCtx, pThis->idCcd, PhysX86Addr, pvVal, cbVal);
}
int PSPProxyCtxPspX86MmioWrite(PSPPROXYCTX hCtx, X86PADDR PhysX86Addr, uint32_t cbVal, const void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspX86MmioWrite(pThis->hPduCtx, pThis->idCcd, PhysX86Addr, pvVal, cbVal);
}
int PSPProxyCtxPspSvcCall(PSPPROXYCTX hCtx, uint32_t idxSyscall, uint32_t u32R0, uint32_t u32R1, uint32_t u32R2, uint32_t u32R3, uint32_t *pu32R0Return)
{
PPSPPROXYCTXINT pThis = hCtx;
#if 0 /** @todo Reintroduce again. */
if (pThis->pProv->pfnCtxPspSvcCall)
return pThis->pProv->pfnCtxPspSvcCall((PSPPROXYPROVCTX)&pThis->abProvCtx[0], pThis->idCcd,
idxSyscall, u32R0, u32R1, u32R2, u32R3, pu32R0Return);
#endif
return -1;
}
int PSPProxyCtxPspAddrXfer(PSPPROXYCTX hCtx, PCPSPPROXYADDR pPspAddr, uint32_t fFlags, size_t cbStride, size_t cbXfer, void *pvLocal)
{
PPSPPROXYCTXINT pThis = hCtx;
if ( cbStride != 1
&& cbStride != 2
&& cbStride != 4)
return -1;
if (cbXfer % cbStride != 0)
return -1;
uint32_t fOp = fFlags & PSPPROXY_CTX_ADDR_XFER_F_OP_MASK_VALID; /* Only set flag is allowed. */
if ( (fOp & PSPPROXY_CTX_ADDR_XFER_F_READ) != PSPPROXY_CTX_ADDR_XFER_F_READ
&& (fOp & PSPPROXY_CTX_ADDR_XFER_F_WRITE) != PSPPROXY_CTX_ADDR_XFER_F_WRITE
&& (fOp & PSPPROXY_CTX_ADDR_XFER_F_MEMSET) != PSPPROXY_CTX_ADDR_XFER_F_MEMSET)
return -1;
return pspStubPduCtxPspAddrXfer(pThis->hPduCtx, pThis->idCcd, pPspAddr, fFlags, cbStride, cbXfer, pvLocal);
}
int PSPProxyCtxPspCoProcWrite(PSPPROXYCTX hCtx, uint8_t idCoProc, uint8_t idCrn, uint8_t idCrm, uint8_t idOpc1, uint8_t idOpc2,
uint32_t u32Val)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspCoProcWrite(pThis->hPduCtx, pThis->idCcd, idCoProc, idCrn, idCrm, idOpc1, idOpc2, u32Val);
}
int PSPProxyCtxPspCoProcRead(PSPPROXYCTX hCtx, uint8_t idCoProc, uint8_t idCrn, uint8_t idCrm, uint8_t idOpc1, uint8_t idOpc2,
uint32_t *pu32Val)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspCoProcRead(pThis->hPduCtx, pThis->idCcd, idCoProc, idCrn, idCrm, idOpc1, idOpc2, pu32Val);
}
int PSPProxyCtxPspWaitForIrq(PSPPROXYCTX hCtx, uint32_t *pidCcd, bool *pfIrq, bool *pfFirq, uint32_t cWaitMs)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspWaitForIrq(pThis->hPduCtx, pidCcd, pfIrq, pfFirq, cWaitMs);
}
int PSPProxyCtxX86SmnRead(PSPPROXYCTX hCtx, uint16_t idNode, SMNADDR uSmnAddr, uint32_t cbVal, void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
if (cbVal != 4)
return -1;
if (pThis->pProv->pfnCtxX86SmnRead)
return pThis->pProv->pfnCtxX86SmnRead((PSPPROXYPROVCTX)&pThis->abProvCtx[0], idNode, uSmnAddr, cbVal, pvVal);
return -1;
}
int PSPProxyCtxX86SmnWrite(PSPPROXYCTX hCtx, uint16_t idNode, SMNADDR uSmnAddr, uint32_t cbVal, const void *pvVal)
{
PPSPPROXYCTXINT pThis = hCtx;
if (cbVal != 4)
return -1;
if (pThis->pProv->pfnCtxX86SmnWrite)
return pThis->pProv->pfnCtxX86SmnWrite((PSPPROXYPROVCTX)&pThis->abProvCtx[0], idNode, uSmnAddr, cbVal, pvVal);
return -1;
}
int PSPProxyCtxX86MemAlloc(PSPPROXYCTX hCtx, uint32_t cbMem, R0PTR *pR0KernVirtual, X86PADDR *pPhysX86Addr)
{
PPSPPROXYCTXINT pThis = hCtx;
if (!pR0KernVirtual && !pPhysX86Addr)
return -1;
if (pThis->pProv->pfnCtxX86MemAlloc)
return pThis->pProv->pfnCtxX86MemAlloc((PSPPROXYPROVCTX)&pThis->abProvCtx[0], cbMem, pR0KernVirtual, pPhysX86Addr);
return -1;
}
int PSPProxyCtxX86MemFree(PSPPROXYCTX hCtx, R0PTR R0KernVirtual)
{
PPSPPROXYCTXINT pThis = hCtx;
if (pThis->pProv->pfnCtxX86MemFree)
return pThis->pProv->pfnCtxX86MemFree((PSPPROXYPROVCTX)&pThis->abProvCtx[0], R0KernVirtual);
return -1;
}
int PSPProxyCtxX86MemRead(PSPPROXYCTX hCtx, void *pvDst, R0PTR R0KernVirtualSrc, uint32_t cbRead)
{
PPSPPROXYCTXINT pThis = hCtx;
if (pThis->pProv->pfnCtxX86MemRead)
return pThis->pProv->pfnCtxX86MemRead((PSPPROXYPROVCTX)&pThis->abProvCtx[0], pvDst, R0KernVirtualSrc, cbRead);
return -1;
}
int PSPProxyCtxX86MemWrite(PSPPROXYCTX hCtx, R0PTR R0KernVirtualDst, const void *pvSrc, uint32_t cbWrite)
{
PPSPPROXYCTXINT pThis = hCtx;
#if 0 /** @todo Reintroduce again. */
if (pThis->pProv->pfnCtxX86MemWrite)
return pThis->pProv->pfnCtxX86MemWrite((PSPPROXYPROVCTX)&pThis->abProvCtx[0], R0KernVirtualDst, pvSrc, cbWrite);
#endif
return -1;
}
int PSPProxyCtxX86PhysMemRead(PSPPROXYCTX hCtx, void *pvDst, X86PADDR PhysX86AddrSrc, uint32_t cbRead)
{
PPSPPROXYCTXINT pThis = hCtx;
if (pThis->pProv->pfnCtxX86PhysMemRead)
return pThis->pProv->pfnCtxX86PhysMemRead((PSPPROXYPROVCTX)&pThis->abProvCtx[0], pvDst, PhysX86AddrSrc, cbRead);
return -1;
}
int PSPProxyCtxX86PhysMemWrite(PSPPROXYCTX hCtx, X86PADDR PhysX86AddrDst, const void *pvSrc, uint32_t cbWrite)
{
PPSPPROXYCTXINT pThis = hCtx;
if (pThis->pProv->pfnCtxX86PhysMemWrite)
return pThis->pProv->pfnCtxX86PhysMemWrite((PSPPROXYPROVCTX)&pThis->abProvCtx[0], PhysX86AddrDst, pvSrc, cbWrite);
return -1;
}
int PSPProxyCtxEmuWaitForWork(PSPPROXYCTX hCtx, uint32_t *pidCmd, X86PADDR *pPhysX86AddrCmdBuf, uint32_t msWait)
{
PPSPPROXYCTXINT pThis = hCtx;
if (pThis->pProv->pfnCtxEmuWaitForWork)
return pThis->pProv->pfnCtxEmuWaitForWork((PSPPROXYPROVCTX)&pThis->abProvCtx[0], pidCmd, pPhysX86AddrCmdBuf, msWait);
return -1;
}
int PSPProxyCtxEmuSetResult(PSPPROXYCTX hCtx, uint32_t uResult)
{
PPSPPROXYCTXINT pThis = hCtx;
if (pThis->pProv->pfnCtxEmuSetResult)
return pThis->pProv->pfnCtxEmuSetResult((PSPPROXYPROVCTX)&pThis->abProvCtx[0], uResult);
return -1;
}
int PSPProxyCtxScratchSpaceAlloc(PSPPROXYCTX hCtx, size_t cbAlloc, PSPADDR *pPspAddr)
{
PPSPPROXYCTXINT pThis = hCtx;
if (!pThis->fScratchSpaceMgrInit)
{
int rc = pspProxyCtxScratchSpaceMgrInit(pThis);
if (rc)
return rc;
}
/** @todo Align size on 8 byte boundary maybe.
* This is a very very simple "heap" manager (doesn't really deserve the name),
* enough for our purpose but don't have to high expectations on it...
*/
/* Find the most optimal chunk first (best match). */
PPSPSCRATCHCHUNKFREE pChunkBest = NULL;
PPSPSCRATCHCHUNKFREE pChunkCur = pThis->pScratchFreeHead;
while (pChunkCur)
{
if ( pChunkCur->cbChunk >= cbAlloc
&& ( !pChunkBest
|| pChunkBest->cbChunk > pChunkCur->cbChunk))
{
pChunkBest = pChunkCur;
/* No point in going further in case of an exact match. */
if (pChunkBest->cbChunk == cbAlloc)
break;
}
pChunkCur = pChunkCur->pNext;
}
if (pChunkBest)
{
if (pChunkBest->cbChunk == cbAlloc)
{
/* Remove free chunk from list. */
if (pChunkBest->pPrev)
pChunkBest->pPrev->pNext = pChunkBest->pNext;
else
pThis->pScratchFreeHead = pChunkBest->pNext;
if (pChunkBest->pNext)
pChunkBest->pNext->pPrev = pChunkBest->pPrev;
*pPspAddr = pChunkBest->PspAddrStart;
free(pChunkBest);
}
else
{
/* Resize chunk and leave everything else in place. */
size_t cbLeft = pChunkBest->cbChunk - cbAlloc;
*pPspAddr = pChunkBest->PspAddrStart + cbLeft;
pChunkBest->cbChunk = cbLeft;
}
return 0;
}
return -1;
}
int PSPProxyCtxScratchSpaceFree(PSPPROXYCTX hCtx, PSPADDR PspAddr, size_t cb)
{
PPSPPROXYCTXINT pThis = hCtx;
/** @todo Align size on 8 byte boundary when done in the alloc method too. */
if (!pThis->pScratchFreeHead)
{
/* No free chunk left, create the first one. */
PPSPSCRATCHCHUNKFREE pChunk = (PPSPSCRATCHCHUNKFREE)malloc(sizeof(*pChunk));
if (!pChunk)
return -1;
pChunk->pNext = NULL;
pChunk->pPrev = NULL;
pChunk->PspAddrStart = PspAddr;
pChunk->cbChunk = cb;
pThis->pScratchFreeHead = pChunk;
}
else
{
/* Find the right chunk to append to. */
PPSPSCRATCHCHUNKFREE pChunkCur = pThis->pScratchFreeHead;
while (pChunkCur)
{
/* Check whether we can append or prepend the memory to the chunk. */
if (PspAddr + cb == pChunkCur->PspAddrStart)
{
/* Prepend and check whether we can merge the previous and current chunk. */
pChunkCur->PspAddrStart = PspAddr;
pChunkCur->cbChunk += cb;
if ( pChunkCur->pPrev
&& pChunkCur->pPrev->PspAddrStart + pChunkCur->pPrev->cbChunk == pChunkCur->PspAddrStart)
{
/* Merge */
PPSPSCRATCHCHUNKFREE pPrev = pChunkCur->pPrev;
pPrev->cbChunk += pChunkCur->cbChunk;
pPrev->pNext = pChunkCur->pNext;
if (pChunkCur->pNext)
pChunkCur->pNext->pPrev = pPrev;
free(pChunkCur);
}
break;
}
else if (pChunkCur->PspAddrStart + pChunkCur->cbChunk == PspAddr)
{
/* Append and check whether we can merge the next and current chunk. */
pChunkCur->cbChunk += cb;
if ( pChunkCur->pNext
&& pChunkCur->PspAddrStart + pChunkCur->cbChunk == pChunkCur->pNext->PspAddrStart)
{
/* Merge */
PPSPSCRATCHCHUNKFREE pNext = pChunkCur->pNext;
pNext->cbChunk += pChunkCur->cbChunk;
pNext->pPrev = pChunkCur->pPrev;
if (pChunkCur->pPrev)
pChunkCur->pPrev->pNext = pNext;
else
pThis->pScratchFreeHead = pNext;
free(pChunkCur);
}
break;
}
else if ( !pChunkCur->pNext
|| ( pChunkCur->PspAddrStart + pChunkCur->cbChunk < PspAddr
&& pChunkCur->pNext->PspAddrStart > PspAddr))
{
/* Insert/Append a new chunk (correct ordering). */
PPSPSCRATCHCHUNKFREE pChunk = (PPSPSCRATCHCHUNKFREE)malloc(sizeof(*pChunk));
if (!pChunk)
return -1;
pChunk->pNext = NULL;
pChunk->pPrev = NULL;
pChunk->PspAddrStart = PspAddr;
pChunk->cbChunk = cb;
PPSPSCRATCHCHUNKFREE pNext = pChunkCur->pNext;
pChunkCur->pNext = pChunk;
pChunk->pPrev = pChunkCur;
if (pNext)
{
pNext->pPrev = pChunk;
pChunk->pNext = pNext;
}
break;
}
pChunkCur = pChunkCur->pNext;
}
}
return 0;
}
int PSPProxyCtxCodeModLoad(PSPPROXYCTX hCtx, const void *pvCm, size_t cbCm)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspCodeModLoad(pThis->hPduCtx, pThis->idCcd, pvCm, cbCm);
}
int PSPProxyCtxCodeModExec(PSPPROXYCTX hCtx, uint32_t u32Arg0, uint32_t u32Arg1, uint32_t u32Arg2, uint32_t u32Arg3,
uint32_t *pu32CmRet, uint32_t cMillies)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxPspCodeModExec(pThis->hPduCtx, pThis->idCcd, u32Arg0, u32Arg1, u32Arg2, u32Arg3,
pu32CmRet, cMillies);
}
int PSPProxyCtxBranchTo(PSPPROXYCTX hCtx, PSPPADDR PspAddrPc, bool fThumb, uint32_t *pau32Gprs)
{
PPSPPROXYCTXINT pThis = hCtx;
return pspStubPduCtxBranchTo(pThis->hPduCtx, pThis->idCcd, PspAddrPc, fThumb, pau32Gprs);
}