Skip to content

Commit

Permalink
Added SetGP,ReplaceGP,GetGP,SetModuleGP macros to defs.h, changed sma…
Browse files Browse the repository at this point in the history
…p to use new GP macros, changed USBD to use GP macros (as per the original).

When used with official software, the software may expect GP support to work.

Note: the compile-time G parameter is currently set to 19, otherwise the export table will cause an error.
I have not figured out how to solve this yet.
  • Loading branch information
sp193 committed Jul 13, 2018
1 parent f569e3d commit 587cad4
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 33 deletions.
36 changes: 36 additions & 0 deletions iop/kernel/include/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@
#define KSEG1 0xa0000000
#define KSEG1ADDR(a) ((__typeof__(a))(((u32)(a) & 0x1fffffff) | KSEG1))

static __inline__ void *ChangeGP(void *gp)
{
void *OldGP;

__asm__ volatile( "move %0, $gp\n"
"move $gp, %1"
: "=&r"(OldGP)
: "r"(gp)
: "gp");

return OldGP;
}

static __inline__ void SetGP(void *gp)
{
__asm__ volatile( "move $gp, %0"
:
: "r"(gp)
: "gp");
}

extern void *_gp;
#define SetModuleGP() ChangeGP(&_gp)

static __inline__ void *GetGP(void)
{
void *gp;

__asm__ volatile( "move $gp, %0"
: "=&r"(gp)
:
: "gp");

return gp;
}

static inline void *iop_memcpy(void *dest, const void *src, int size)
{
u8 *d = (u8 *)dest, *s = (u8 *)src;
Expand Down
12 changes: 0 additions & 12 deletions iop/network/smap/src/include/main.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//In the SONY original, all the calls to DEBUG_PRINTF() were to sceInetPrintf().
#define DEBUG_PRINTF(args...) printf(args)

/*
Sorry, but even I can't explain the syntax used here. :(
I know that _ori_gp has to be "early-clobbered" and the GP register will get clobbered... but I don't really know why GCC can't determine which registers it can and can't use automatically. And I don't really understand what "clobbering" registers is.
*/
#define SaveGP() \
void *_ori_gp; \
__asm volatile("move %0, $gp\n" \
"move $gp, %1" :"=&r"(_ori_gp): "r"(&_gp) : "gp")

#define RestoreGP() \
__asm volatile("move $gp, %0" :: "r"(_ori_gp) : "gp")

struct RuntimeStats{
u32 RxDroppedFrameCount;
u32 RxErrorCount;
Expand Down
36 changes: 24 additions & 12 deletions iop/network/smap/src/smap.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <defs.h>
#include <errno.h>
#include <stdio.h>
#include <dmacman.h>
Expand Down Expand Up @@ -63,8 +64,6 @@ static unsigned int EnableAutoNegotiation=1;
static unsigned int EnablePinStrapConfig=0;
static unsigned int SmapConfiguration=0x5E0;

extern void *_gp;

int DisplayBanner(void){
printf("SMAP (%s)\n", VersionString);
return MODULE_NO_RESIDENT_END;
Expand Down Expand Up @@ -497,12 +496,14 @@ static void IntrHandlerThread(struct SmapDriverData *SmapDrivPrivData){
}

static int Dev9IntrCb(int flag){
SaveGP();
void *OldGP;

OldGP = SetModuleGP();

dev9IntrDisable(DEV9_SMAP_ALL_INTR_MASK);
iSetEventFlag(SmapDriverData.Dev9IntrEventFlag, SMAP_EVENT_INTR);

RestoreGP();
SetGP(OldGP);

return 0;
}
Expand Down Expand Up @@ -536,18 +537,26 @@ static void Dev9PostDmaCbHandler(int bcr, int dir){
}

int SMAPStart(void){
SaveGP();
void *OldGP;

OldGP = SetModuleGP();

SetEventFlag(SmapDriverData.Dev9IntrEventFlag, SMAP_EVENT_START);
RestoreGP();

SetGP(OldGP);

return 0;
}

void SMAPStop(void){
SaveGP();
void *OldGP;

OldGP = SetModuleGP();

SetEventFlag(SmapDriverData.Dev9IntrEventFlag, SMAP_EVENT_STOP);
SmapDriverData.NetDevStopFlag=1;
RestoreGP();

SetGP(OldGP);
}

static void ClearPacketQueue(struct SmapDriverData *SmapDrivPrivData){
Expand All @@ -566,7 +575,9 @@ static void ClearPacketQueue(struct SmapDriverData *SmapDrivPrivData){
}

void SMAPXmit(void){
SaveGP();
void *OldGP;

OldGP = SetModuleGP();

if(SmapDriverData.LinkStatus){
if(QueryIntrContext())
Expand All @@ -578,7 +589,7 @@ void SMAPXmit(void){
ClearPacketQueue(&SmapDriverData);
}

RestoreGP();
SetGP(OldGP);
}

static inline int SMAPGetLinkMode(void){
Expand Down Expand Up @@ -655,8 +666,9 @@ static inline int SMAPGetLinkStatus(void){

int SMAPIoctl(unsigned int command, void *args, unsigned int args_len, void *output, unsigned int length){
int result;
void *OldGP;

SaveGP();
OldGP = SetModuleGP();

switch(command){
case NETMAN_NETIF_IOCTL_ETH_GET_MAC:
Expand Down Expand Up @@ -705,7 +717,7 @@ int SMAPIoctl(unsigned int command, void *args, unsigned int args_len, void *out
result=-1;
}

RestoreGP();
SetGP(OldGP);

return result;
}
Expand Down
1 change: 0 additions & 1 deletion iop/network/smap/src/xfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "xfer.h"

extern void *_gp;
extern struct SmapDriverData SmapDriverData;

static int SmapDmaTransfer(volatile u8 *smap_regbase, void *buffer, unsigned int size, int direction){
Expand Down
1 change: 1 addition & 0 deletions iop/usb/usbd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Review ps2sdk README & LICENSE files for further details.

IOP_OBJS = hcd.o hub.o interface.o mem.o usbd.o usbio.o driver.o imports.o exports.o
IOP_CFLAGS += -mgpopt -G19

include $(PS2SDKSRC)/Defs.make
include $(PS2SDKSRC)/iop/Rules.bin.make
Expand Down
14 changes: 10 additions & 4 deletions iop/usb/usbd/src/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "mem.h"
#include "hub.h"

#include "defs.h"
#include "stdio.h"
#include "sysclib.h"
#include "thbase.h"
Expand All @@ -31,11 +32,12 @@ int callbackEvent;

int callUsbDriverFunc(int (*func)(int devId), int devId, void *gp) {
int res;

if (func) {
usbdUnlock();
// move gp..
usbdUnlock();
ChangeGP(gp);
res = func(devId);
// restore gp...
SetGP(&_gp);
usbdLock();
return res;
} else
Expand Down Expand Up @@ -172,6 +174,7 @@ void callbackThreadFunc(void *arg) {
int intrStat;
IoRequest *req;
IoRequest reqCopy;

while (1) {
WaitEventFlag(callbackEvent, 1, WEF_CLEAR | WEF_OR, &eventRes);
do {
Expand All @@ -197,9 +200,12 @@ void callbackThreadFunc(void *arg) {
freeIoRequest(req);
usbdUnlock();

// add GP stuff
if (reqCopy.userCallbackProc)
{
SetGP(req->gpSeg);
reqCopy.userCallbackProc(reqCopy.resultCode, reqCopy.transferedBytes, reqCopy.userCallbackArg);
SetGP(&_gp);
}
}
} while (req);
}
Expand Down
3 changes: 2 additions & 1 deletion iop/usb/usbd/src/hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "usbio.h"
#include "driver.h"

#include "defs.h"
#include "stdio.h"
#include "sysclib.h"
#include "sysmem.h"
Expand Down Expand Up @@ -58,7 +59,7 @@ int initHubDriver(void) {
}
hub->next = NULL;

doRegisterDriver(&HubDriver, NULL); // gp, actually
doRegisterDriver(&HubDriver, &_gp);
return 0;
}

Expand Down
Loading

2 comments on commit 587cad4

@Jay-Jay-OPL
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sp193 I just wanted to report that I think this PS2SDK commit: 587cad4 (Jul 13, 2018) broke OPL (ifcaro repo 1085-Beta-4b9eff4) from running.

The last PS2SDK commit that works is this: f569e3d (Jul 11, 2018).

I then tested a few other PS2SDK commits and here is my report:

//tested ps2sdk commits:
db7098a (Jul 14, 2018) = COMPILES BUT FAILS TO RUN OPL
ebabf0f (Jul 14, 2018) = COMPILES BUT FAILS TO RUN OPL
587cad4 (Jul 13, 2018) = COMPILES BUT FAILS TO RUN OPL
f569e3d (Jul 11, 2018) = GOOD
78ba532 (Jul 9, 2018) = GOOD
22acb18 (Jul 4, 2018 ) = FAILS TO COMPILE OPL -- SHOOTS OUT ERROR
9b69633 (Jul 2, 2018) = GOOD
93f6aad (Jun 28, 2018) = GOOD

Let me know if you have any questions or suggestions?

NOTE: I use a Windows Compiling Environment. Here is the guide I follow: http://www.ps2-home.com/forum/viewtopic.php?f=50&t=169

NOTE 2: I used wLE v4.43a Jul 4, 2018 commit: 59a4962 to run OPL's ELF from either MASS or MC.

@sp193
Copy link
Member Author

@sp193 sp193 commented on 587cad4 Jul 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in e09a538.

Please sign in to comment.