Syscalls and Interrupts

Discuss the development of software, tools, libraries and anything else that helps make ps2dev happen.

Moderators: cheriff, Herben

Post Reply
misfire
Posts: 110
Joined: Mon Sep 06, 2004 7:53 am
Location: Germany

Syscalls and Interrupts

Post by misfire »

With SetSyscall(s32 syscall_num, void* handler), what is the memory address where the pointer to the handler will be written to? I know that the exception service routine for syscalls resides at 0x80000180, but is there some kind of vector table used for all syscalls?

Also, what is the general difference between an interrupt and a syscall? I've got some experience in x86 assembly and never heard of syscalls...
mharris
Posts: 155
Joined: Sun Jan 25, 2004 2:26 pm
Location: Annapolis, MD, USA

Post by mharris »

An interrupt is typically generated by an external event (most CPUs have an INTR or similar pin). On the PC, which is more general purpose, you have the various interrupt request levels which any hardware device can claim (e.g., IRQ3). The EE has stuff a little more hard-wired, as far as I can tell, so there are interrupts for VSync, DMA, etc. Although, I assume there's probably a way to 'rewire' things on the EE by messing around with the interrupt controller.

A system call, or syscall, is a 'trap' into the kernel -- it's typically an instruction that jumps from user-mode code to kernel mode. With Linux/i386, this is via a "software interrupt" instruction (INT 0x80), on the PS2, it's via a dedicated SYSCALL instruction. The x86 architecture doesn't have a SYSCALL instruction, but may processors have something similar (e.g., the M68K has a TRAP instruction), but the x86 software interrupts pretty much serve the same purpose, since very few PCs have 256 IRQ levels...

The 'EE Core' manual might have more info about remapping system calls, interrupt handlers, etc. but it's unlikely that you'll be able to do anything useful besides breaking the kernel.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Actually, the AMD64 added a fast SYSCALL instruction. 64bit linux for the AMD64 uses it for system calls. I would imagine that Windows XP64 does as well.
Guest

Re: Syscalls and Interrupts

Post by Guest »

misfire wrote: Also, what is the general difference between an interrupt and a syscall? I've got some experience in x86 assembly and never heard of syscalls...
A syscall is merely one type of exception. An interrupt is also a type of exception. Interrupts are normally created by hardware devices on or external to the CPU. A syscall is a software generated exception.

As others have mentioned, the primary purpose of syscall is to jump from user code to kernel level code in order to perform some lower-level function in a more protected manner. This is how operating systems maintain certain protections by forcing all kernel<->user code interactions through the syscall interface.

I am not familiar with x86 assembly, but I imagine that if it doesn't have a function specificaly called syscall then there is a similar instruction. The different protected mode priviledge rings of the x86 arch require some method to move amongst them I imagine.
misfire
Posts: 110
Joined: Mon Sep 06, 2004 7:53 am
Location: Germany

Post by misfire »

Thanks for your explanations.

In the meantime, I found a way to determine the location of the syscall vector table. (I think this address varies between different BIOS versions.)

Code: Select all

u32 getSyscallVTblAddr&#40;void&#41;
&#123;
	const s32 syscall_num&#91;2&#93; = &#123; 0xFE, 0xFF &#125;;
	const u32 magic&#91;2&#93; = &#123; 0x1337C0DE, 0xB16B00B5 &#125;;
	u32 addr = -1;
	u32 i;

	SetSyscall&#40;syscall_num&#91;0&#93;, &#40;void*&#41;magic&#91;0&#93;&#41;;
	SetSyscall&#40;syscall_num&#91;1&#93;, &#40;void*&#41;magic&#91;1&#93;&#41;;
	ee_kmode_enter&#40;&#41;;

	for &#40;i = 0x80000000; i < 0x80080000; i += 4&#41; &#123;
		if &#40;!memcmp&#40;&#40;u32*&#41;i, magic, sizeof&#40;magic&#41;&#41;&#41; &#123;
			addr = i - syscall_num&#91;0&#93; * 4;
			break;
		&#125;
	&#125;

	ee_kmode_exit&#40;&#41;;
	SetSyscall&#40;syscall_num&#91;0&#93;, &#40;void*&#41;0&#41;;
	SetSyscall&#40;syscall_num&#91;1&#93;, &#40;void*&#41;0&#41;;

	return addr;
&#125;
This is only interesting, if you won't to "hook" a syscall in order to extend its functionality.

EDIT: Optimized code above. Now, it seems to work for every bios.
misfire
Posts: 110
Joined: Mon Sep 06, 2004 7:53 am
Location: Germany

Post by misfire »

Talking about interrupts, what is the difference between _EnableIntc() and _iEnableIntc()?
Too bad that there is no documentation on the EE syscalls. :(

Another thing. In "libcdvd.h" it says:

Code: Select all

// search for a file on disc
// 
// args&#58;	file structure to get file info in
//			name of file to search for &#40;no wildcard characters&#41;
//				&#40;should be in the form '\\SYSTEM.CNF;1'&#41;
// returns&#58;	1 if successful
//			0 if error &#40;or no file found&#41;
s32  cdSearchFile&#40;CdvdFileSpec_t *file, const char *name&#41;;
With the sample filename "\\SYSTEM.CNF;1", what's the purpose of ";1"?
User avatar
Drakonite
Site Admin
Posts: 990
Joined: Sat Jan 17, 2004 1:30 am
Contact:

Post by Drakonite »

misfire wrote:Talking about interrupts, what is the difference between _EnableIntc() and _iEnableIntc()?
Too bad that there is no documentation on the EE syscalls. :(

Another thing. In "libcdvd.h" it says:

Code: Select all

// search for a file on disc
// 
// args&#58;	file structure to get file info in
//			name of file to search for &#40;no wildcard characters&#41;
//				&#40;should be in the form '\\SYSTEM.CNF;1'&#41;
// returns&#58;	1 if successful
//			0 if error &#40;or no file found&#41;
s32  cdSearchFile&#40;CdvdFileSpec_t *file, const char *name&#41;;
With the sample filename "\\SYSTEM.CNF;1", what's the purpose of ";1"?
It's part of the iso9660 spec and is usually 'filtered out' by the low level drivers.
Shoot Pixels Not People!
Makeshift Development
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

misfire wrote:With the sample filename "\\SYSTEM.CNF;1", what's the purpose of ";1"?
It's part of the ISO9660 standard for filenames. All files have that stuck on the end. Why? Well, the FS that derives from allows any number of files with the same name. Each one is considered a version of the original file and the number indicates the version. In this case, there is only the original, or ";1" file. Doing it that way made handling CDs easier for the systems using that FS.
misfire
Posts: 110
Joined: Mon Sep 06, 2004 7:53 am
Location: Germany

Post by misfire »

Ok, thanks for your replies.

Those i* functions are used in interrupt mode of course.

Today I've got some questions about threads. I figured out that the thread priority must be a value between 0 and 127. So, does this mean that the PS2 can handle up to 128 threads? With 0 being the highest and 0x7F the lowest priority?

Also, what does the function RotateThreadReadyQueue() do?
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

You can have any number of threads with the same priority. Threads at the same priority which are ready to run share the CPU time in round-robin format. RotateThreadReadyQueue() forces the calling thread to be descheduled. The next ready thread will be run.
Post Reply