Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with stack and heap #56

Merged
merged 1 commit into from
Apr 1, 2024
Merged

Conversation

fjtrujy
Copy link
Member

@fjtrujy fjtrujy commented Mar 31, 2024

Description

This PR solves the current issue we are suffering with ps2link which is not able to load config from IPCONFIG.DAT.
The reason why config file couldn't be loaded, it is because it fails when using open IPCONFIG.DAT file, as it fails when running malloc.
After deeper investigation, I figured out that heap wasn't set-up properly.

When using the default linkfile we have:

	PROVIDE(_heap_size = -1);
	PROVIDE(_stack = -1);
	PROVIDE(_stack_size = 128 * 1024);

Then during crt0.c we are calling to kernel the SetupThread and SetupHeap functions, which set the stack and heap respectively.
PS2BIOS when _stack == -1, it sets the stack to the end of the memory region (minus stack size + small GAP).
Similar stuff happens when _heap_size == -1, it sets the heap from _end to _stack.

At the end we have a picture similar to:

image

However, ps2link is not a common app, it needs to be executed in a custom memory region, we need to make sure that program + stack + heap everything fits within a concrete memory region.

With the current linkfile used in ps2link we were making one issue:

        PROVIDE(_heap_size = -1);
	PROVIDE(_stack = .);
	PROVIDE(_stack_size = ORIGIN(MAIN_REGION) + LENGTH(MAIN_REGION) - _stack);

We were using the whole available space for stack (this is right if you know that your program will never use the heap).

The fix that I have applied here is to set a fixed stack_size and also calculate manually stack pointer. This will force the stack to be in a concrete region, and then let the remaining available memory region to be used as heap.

In order to find how to fix it I have been taking a look at the implementation of the kernel calls from this repo:
https://github.com/AKuHAK/fps2bios/blob/master/kernel/eeload/eekernel.c#L2859-L2914

Also have in mind that highloading version won't work properly with ELF using default linkfile, as the ELF's stack memory region will clash with ps2link highload version.

Cheers

@fjtrujy fjtrujy requested a review from rickgaiser March 31, 2024 20:58
@fjtrujy fjtrujy merged commit 0269a95 into ps2dev:master Apr 1, 2024
4 checks passed
@10522
Copy link

10522 commented Apr 5, 2024

This PR solves the current issue we are suffering with ps2link which is not able to load config from IPCONFIG.DAT

@fjtrujy, this issue is still not fixed. I tested 1.9.1 and Development build.

@fjtrujy
Copy link
Member Author

fjtrujy commented Apr 5, 2024

This PR solves the current issue we are suffering with ps2link which is not able to load config from IPCONFIG.DAT

@fjtrujy, this issue is still not fixed. I tested 1.9.1 and Development build.

You must put your ps2link in MC0 or MC1, otherwise it won’t work.

@10522
Copy link

10522 commented Apr 12, 2024

@fjtrujy, PS2Link 1.9.1 works via MC, but PS2Link 1.7.0 even works via USB. Can this be fixed in PS2Link 1.9.1?

@fjtrujy
Copy link
Member Author

fjtrujy commented Apr 13, 2024

@fjtrujy, PS2Link 1.9.1 works via MC, but PS2Link 1.7.0 even works via USB. Can this be fixed in PS2Link 1.9.1?

Could you try binary generated here?
https://github.com/ps2dev/ps2link/actions/runs/8672568040

@10522
Copy link

10522 commented Apr 21, 2024

@fjtrujy, latest Development build works via USB.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants