28

To execute a system call, a program must execute a special trap instruction.

Why is it called a "trap" instruction? What is the etymology of this usage of the word "trap"?

Is it related to the usual English word trap? Is something is getting "trapped" as in "unable to escape"?

5
  • 5
    Why the downvote? Is there a better stackexchange site for this question? I looked for a "history of computing" stackexchange and didn't find one. I don't think the "English language" stackexchange is right because this is specific to computing.
    – littleO
    Nov 18, 2020 at 10:20
  • 2
    I wouldn't vote to close it. I like historical questions about computing, and I don't think "opinion-based" is a good justification for a close vote if sufficient evidence of the etymology of the word can be provided. I, for one, am curious about what answers will be given to that one. :)
    – haylem
    Nov 18, 2020 at 15:24
  • 6
    This might be better on retrocomputing.stackexchange.com, as you're looking for historical information.
    – Erik Eidt
    Nov 18, 2020 at 23:18
  • Think of mouse trap and similar & its explosive behavior. The program counter reaching that instruction is catched to do something something abruptly different, possibly with mode change.
    – Joop Eggen
    Dec 15, 2020 at 21:22
  • Somewhat off-topic, but not all OS's use/used a dedicated trap instruction for syscalls: devblogs.microsoft.com/oldnewthing/20041215-00/?p=37003
    – BCS
    Dec 17, 2020 at 18:25

6 Answers 6

16

It's what we call kernel or system "trap", which triggers a kernel mode switch to execute the system call.

As to why that word was used, I haven't found definitive proof yet, so my current assumption is that it comes from either or both of these 2 options:

  • We used to say sometimes that the code "fell" into a different mode instead of "switching", which resembles the mental image of "falling into a trap".

  • A "trap" is meant to capture something, here a reaction to an interrupt, which can happen in case of a failure (which can have several levels of severity, including being "fatal").

Furthermore, and along the same lines of thinking, we usually think of a program's control flow as having a "normal" flow and having an "error" or "exceptional" flow for things that should not (or rarely) happen. And the point of the "trap" is to capture such events.

But I think the sentence you quote is strangely worded.

4
  • 2
    I agree. I think it started with „capturing“ or „intercepting“ special conditions in the code (I.e. division by zero) and then the same mechanism (address tables for indirect jumps) was used for other purposes. Then instructions were included to support compilers to generate „capture code“ for their own special conditions and finally it is used as „normal“ subroutine calls that require full control by the OS. The instruction set of of the MC68k is a perfect example for all of that. With that context in mind, I think, the quote does not sound strange. Dec 9, 2020 at 7:19
  • 1
    Motorla 68000 had a 'trap' instruction in Atari ST TOS.
    – Coroos
    Dec 16, 2020 at 9:28
  • That line, I suppose, is coming from the book OS: 3 easy pieces from this chapter pages.cs.wisc.edu/~remzi/OSTEP/cpu-mechanisms.pdf, page 4. I think the trap word used here is because if you want to move from User mode to Kernel mode, you have to call a trap instruction. When you make a system call, the system call calls the trap instruction with given arguments. So, I think the author here is saying that any system call you make, calls the trap instruction inside its implementation.
    – luv.preet
    Jan 18 at 3:24
  • I think there is a snippet from the book explaining this, on page 3 in a greyed area. You can also read it here textbin.xyz/…
    – luv.preet
    Jan 18 at 3:27
8

While we can all say that the concept of "trap on overflow", or "trap on divide by zero" makes intuitive sense – stop the program from proceeding — what I would do is look to the various traditional vendors since terminology from days of yore was not standardized (and perhaps still isn't).

The terms 'exception', 'software interrupt', 'interrupt', 'trap', 'fault' all have very similar meanings in certain environments.

While I don't know who really started it, the term trap is heavily used by Motorola, what with the 6800 (development started 1971, released 1974) and later 68000 processors.

The Intel docs of today use the term but I don't see it in docs for older Intel processors, e.g. 8080. With the Intel 8086 (1976) I see the term trap being used to mean single stepping the processor, which I don't see for the older 8088.

1
  • IBM OS/2 used trap for fatal exception or kernel panic.
    – Gwaptiva
    Dec 8, 2020 at 9:14
4

For me the term "trap" always invoked the notion of suddenly seizing control of something unexpectedly and against its will.

If you think of the "something" as "normal program flow", then that's a pretty good description of what a software trap does.

4

According to Wikipedia, first: the nomenclature of "trap" varies pretty wildly between systems:

There is a wide variation in the nomenclature. On some computers the term trap refers to any interrupt, on some machines to any synchronous interrupt, on some machines to any interrupt not associated with input/output, on some machines only to interrupts caused by instructions with trap in their names, etc.

According to a 1996 article by Randall Hyde cited,

The concept of an interrupt is something that has expanded in scope over the years. The 80x86 family has only added to the confusion surrounding interrupts by introducing the int (software interrupt) instruction. Indeed different manufacturers have used terms like exceptions faults aborts traps and interrupts to describe the phenomena this chapter discusses. Unfortunately there is no clear consensus as to the exact meaning of these terms. Different authors adopt different terms to their own use. While it is tempting to avoid the use of such misused terms altogether for the purpose of discussion it would be nice to have a set of well defined terms we can use in this chapter. Therefore we will pick three of the terms above interrupts traps and exceptions and define them. This chapter attempts to use the most common meanings for these terms but don't be surprised to find other texts using them in different contexts.

In short

A 'trap' is whatever the system in question defines it to be ... but it's generically something akin to an error condition, special system interrupt, or other "unusual" condition.

3

'Trap' because it intercepts an exception. When the CPU issues an "undefined operation" exception, it switches into supervisor mode and checks the low-order part of the instruction for an index into a jump table of routines which implement the desired behavior, for example, software floating point routines. If no such entry point exists, you get the expected kaboom!

1

The evolution and absorption of hardware terminology into modern systems and software usage is an interesting one. For many, the true etymology was lost before many of us were even born. In most cases, the original meanings of words have been adopted to have similar technical meaning. For instance, we don't give "memory" a second thought, despite the fact the mechanisms for achieving data storage and retrieval in hardware varies significantly, and all are substantively different than that of human memory.

In the absence of historical evidence to the contrary, we should look at the English definition of the word trap (emphasis is mine):

1 a contrivance used for catching game or other animals, as a mechanical device that springs shut suddenly.

Consider the electrical engineer (EE) who designed the first trap circuit. What was the design goal and how did they achieve it? I believe it was either the HALT or TRAP instruction, or the effect of the first fault detector; which originally was designed to halt execution of electromechanical computing or control devices when software or hardware detection of any state which could result in damaged hardware or loss of resources, that was the progenitor of these terms into the systems software lexicon.

The first programs ever written, were punched into leather belts that controlled steam driven looms. The primary drive wheel to which the steam piston or cylinder was attached, were often made of Iron or wood and > 8' in diameter (I worked on a mill once, with a 16' cast iron drive wheel) and could not be stopped quickly, due to inertia. If there was a failure (fault) of the leather control belt (the program), a mechanical device would "immediately" disengage the drive belt from the machine to which it was attached, and in some cases, a break would be applied at that machine. In that era, the lexicon was driven by mechanical engineers (ME's) and millwrights, not unfamiliar with the use of mechanical traps for catching game or vermin.

Fast forward about 75 years to that EE designing the first fault detection and trapping circuits back in the late 1930's or early 1940's. I believe a search of the earliest computing hardware patents would probably turn up some of the earliest mentions of faults and traps in relation to computing devices. Most of the earliest systems software lexicon was adopted from that first generation of EE's and their proteges.

1
  • I know that electrical machines can trip in a very similar way to the mechanical ones you describe.
    – user253751
    Dec 15, 2020 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.