0

I am taking an operating systems course. I have just learned about traps.

A trap is an exception in a user process. It's caused by division by zero or invalid memory access. It's also the usual way to invoke a kernel routine (a system call) because those run with a higher priority than user code

I would like to see how traps are implemented, but every source I can find on traps discusses them in the abstract. Are traps implemented in Assembly or C? Can you give example code that implements a trap?

The part of the trap I am most interested in is how I would begin writing my own system calls. An ideal example would be a trap that executes when you want to read a file in Linux.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mark Wallace
  • 528
  • 2
  • 12
  • 1
    I'm afraid this is way too broad to answer. You can check out ["System Calls" on the OSDev wiki](https://wiki.osdev.org/System_Calls) to start learning something about how syscalls can be implemented. – Marco Bonelli Aug 14 '22 at 13:43
  • Also look at processor manuals to see how the CPU can handle various types of exceptions. – sawdust Aug 16 '22 at 08:36

1 Answers1

1

Specifically in x86 trap is implemented as an assembly instruction with opcode ud2. This is seen in the linux kernel under the macro BUG() that is defined in /arch/x86/include/asm/bug.h

#define ASM_UD2     ".byte 0x0f, 0x0b"
#define INSN_UD2    0x0b0f
#define LEN_UD2     2
...
#define BUG()                           \
do {                                \
    instrumentation_begin();                \
    _BUG_FLAGS(ASM_UD2, 0, "");             \
    __builtin_unreachable();                \
} while (0)

In GCC there is also a builtin called __builtin_trap() that inserts an opcode ud2.

Godbolt Link

void doit( int j) {
    if ( j<0 ) {
        __builtin_trap();
    }
}

produces

doit(int):                               # @doit(int)
        test    edi, edi
        js      .LBB0_1
        ret
.LBB0_1:
        ud2

Sanitizers also use trap explicitly to force an abort or stop in the debugger. Clang Link

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Something Something
  • 3,999
  • 1
  • 6
  • 21
  • 1
    *"Specifically in x86 trap is implemented as an assembly instruction with opcode ud2"* - well, not really. A "trap" is a generic concept. The `ud2` instruction will cause an invalid opcode exception, which you could consider a kind of "trap", for which the OS will usually have an exception handler ready. No OS uses `ud2` as a way to implement system calls though, that'd make little to no sense. – Marco Bonelli Aug 14 '22 at 13:40
  • @MarcoBonelli Fair. But gcc uses the name `__builtin_trap()` for a reason. – Something Something Aug 14 '22 at 18:37