Skip to contentSkip to conversation

Gateway instructions

hppa-linux Cary Coutant 3 participants 3 messages Mar 18, 1999 – Mar 19, 1999

lore
Share & tools
lore
#0 lore raw

The PSW B bit is set to indicate that an instruction is executing in the delay slot of another branch. Gateway instructions trap if the B bit is set to prevent a malicious process from using sequence like the following to gain a higher privilege level:

B gateway
B my_routine

Without this protection, the first branch to the gateway instruction would promote the privilege level, but control would immediately be transferred to the user's own code.

Branches in delay slots are tricky; here's what's really happening in the hardware:

PC offset queue after instruction
PC Instruction head tail

user+0 ... user+4 user+8
user+4 B gateway user+8 gateway
user+8 B my_routine gateway my_routine
gateway B,GATE syscall my_routine syscall
my_routine B my_routine+4 syscall my_routine+4
syscall ... my_routine+4 my_routine+8
my_routine+4 ...

When a page's access rights are 4, 5, 6, or 7, a gateway instruction on that page causes the privilege promotion. Most HP-UX system calls are branches to a common gateway instruction on a gateway page in the fourth quadrant. This gateway instruction then branches to a common syscall entry sequence that ultimately switches on the syscall number passed in a GR. Some "lightweight" syscalls may be implemented with their own gateways.

The B bit is not a problem as long as the delay slot of the branch to the gateway is either nullified or filled in with a non-branch instruction. For example,

    LDIL L'GATEWAY,%r1
    BLE  R'GATEWAY(%sr7,%r1)
    LDO  sycallnum,%r22

Cary Coutant
Hewlett-Packard Co.
Application Delivery Lab

#1 lore raw
Quoted 7 lines · Cary Coutant

When a page's access rights are 4, 5, 6, or 7, a gateway instruction on that page causes the privilege promotion. Most HP-UX system calls are branches to a common gateway instruction on a gateway page in the fourth quadrant. This gateway instruction then branches to a common syscall entry sequence that ultimately switches on the syscall number passed in a GR. Some "lightweight" syscalls may be implemented with their own gateways.

OK, so you don't have a spot on the page for each syscall? I was thinking that we'd want to do that, since we're going to use the whole page anyway and it would make things a little quicker by eliminating the later dispatch-by-syscall step.

I presume that the syscall gateway page layout is part of the HP-UX kernel/user ABI though, so we'll be following your lead for SOM/HP-ELF binaries anyway.

Quoted 7 lines

The B bit is not a problem as long as the delay slot of the branch to the gateway is either nullified or filled in with a non-branch instruction. For example,

    LDIL L'GATEWAY,%r1
    BLE  R'GATEWAY(%sr7,%r1)
    LDO  sycallnum,%r22

Someday (maybe today, if I get all my work done =) ), I'll learn to read that.

Mike

Signature · 1 line
368177.00 314980.91
#2 lore raw
Quoted 8 lines
Quoted 8 lines

The B bit is not a problem as long as the delay slot of the branch to the gateway is either nullified or filled in with a non-branch instruction. For example,

    LDIL L'GATEWAY,%r1
    BLE  R'GATEWAY(%sr7,%r1)
    LDO  sycallnum,%r22

Branch to space in %sr7 and offset contained in %r1, also load the syscall number in %r22 in the delay slot of BLE. I guess the assumption here is that %sr7 points to system space. Value of GATEWAY can be replaced by 0xc0000000 or whatever.

A equivalent 80386 sequence to make syscall:

movl $1, %eax ; load syscall num in %eax. in this case
"exit"
int $0x80 ; syscall trap in to kernel

The number 80 is OS dependent and no magic number. Its just that Linux 80386 goes through trap gate 128 (0x80) for syscalls.

pkd

Quoted 7 lines

Someday (maybe today, if I get all my work done =) ), I'll learn to read that.

Mike