Terminate-and-stay-resident program

Source: Wikipedia, the free encyclopedia.

A terminate-and-stay-resident program (commonly TSR) is a computer program running under DOS that uses a system call to return control to DOS as though it has finished, but remains in computer memory so it can be reactivated later.[1] This technique partially overcame DOS's limitation of executing only one program, or task, at a time. TSRs are used only in DOS, not in Windows.

Some TSRs are utility software that a computer user might call up several times a day, while working in another program, using a hotkey. Borland Sidekick was an early and popular example of this type. Others serve as device drivers for hardware that the operating system does not directly support.

Use

Normally DOS can run only one program at a time. When a program finishes, it returns control to DOS using the system call INT 21h/4Ch.[2] The memory and system resources used are then marked as unused. This makes it impossible to restart parts of the program without having to reload it all. However, if a program ends with the system call INT 27h or INT 21h/31h, the operating system does not reuse a certain specified part of its memory.

The original call, INT 27h, is called "terminate but stay resident", hence the name "TSR". Using this call, a program can make up to 64 KB of its memory resident. MS-DOS version 2.0 introduced an improved call, INT 21h/31h ('Keep Process'), which removed this limitation and let the program return an exit code. Before making this call, the program can install one or several interrupt handlers pointing into itself, so that it can be called again. Installing a hardware interrupt vector allows such a program to react to hardware events. Installing a software interrupt vector allows it to be called by the currently running program. Installing a timer interrupt handler allows a TSR to run periodically (see ISA and programmable interval timer, especially the section "IBM PC compatible").

The typical method of using an interrupt vector involves reading its present value (the address), storing it within the memory space of the TSR, and replacing it with an address in its own code. The stored address is called from the TSR, in effect forming a singly linked list of interrupt handlers, also called interrupt service routines, or ISRs. This procedure of installing ISRs is called chaining or hooking an interrupt or an interrupt vector.

By chaining the interrupt vectors TSRs can take complete control of the computer. A TSR can have one of two behaviors:

  • Take complete control of an interrupt by not calling other TSRs that had previously altered the same interrupt vector.
  • Cascade with other TSRs by calling the old interrupt vector. This can be done before or after they executed their actual code. This way TSRs can form a chain where each calls the next.

The terminate-and-stay-resident method is used by most DOS viruses and other malware, which can either take control of the PC or stay in the background. This malware will react to disk I/O or execution events by infecting executable (.EXE or .COM) files when it is run and data files when they are opened.

TSRs can be loaded at any time; either during the DOS startup sequence (for example, from

DEVICE
statements in CONFIG.SYS.

Some TSRs have no way to unload themselves, so they will remain in memory until a reboot. However unloading is possible externally, using utilities like the MARK.EXE/RELEASE.EXE combo by TurboPower Software or soft reboot TSRs which will catch a specific key combination and release all TSRs loaded after them. As the chain of ISRs is singly linked, and a TSR may store the link to its predecessor anywhere it chooses, there is no general way for a TSR to remove itself from the chain. So usually a stub must be left in memory when unloading a TSR, causing memory fragmentation. This problem gave rise to TSR cooperation frameworks such as TesSeRact and AMIS.[3]

Interrupt sharing

To manage problems with many TSRs sharing the same interrupt, a method called Alternate Multiplex Interrupt Specification (AMIS) was proposed by

Interrupt Sharing Protocol, originally invented for sharing hardware interrupts of an x86 processor. AMIS services are available via Int 2Dh.[4]

The proposal never gained a widespread traction among programmers in its days. It existed alongside several other competing specifications of varying sophistication.[5]

Faults

While very useful, or even essential to overcome DOS's limitations, TSRs have a reputation as troublemakers. Many hijack the operating system in varying documented or undocumented ways, often causing systems to crash on their activation or deactivation when used with particular applications or other TSRs. As explained above, some viruses and other malware were coded as TSRs, and are deliberately troublesome. Additionally, in DOS all programs, even those with large amounts of physical RAM, must be loaded into the first 640 KB of RAM (the conventional memory). TSRs are no exception, and take chunks from that 640 KB that are thus unavailable to other applications. This meant that writing a TSR was a challenge of achieving the smallest possible size for it, and checking it for compatibility with a lot of software products from different vendors—often a very frustrating task.

In the late 1980s and early 1990s, many

CLOAKING
).

Return

With the arrival of

MEMMAKER
which try to maximize the available space in the first 640 KB by determining how best to allocate TSRs between low and high memory.

Decline

With the development of games using

Win16 applications can do TSR-like tricks such as patching the interrupt descriptor table
(IDT) because Windows allowed it.

preemptive multitasking, allowing multiple programs and device drivers to run simultaneously without the need for special programming tricks; the kernel
and its modules have been made exclusively responsible for modifying the interrupt table.

See also

References

  1. ^ Maybury, Rick (1998). "Beat the Bug—Computer Viruses". PC Top Tips. Retrieved 2012-02-09.
  2. ^ [1] HelpPC reference: INT 21,0 – Program Terminate
  3. ^ a list of TSR libraries Archived 2007-08-17 at the Wayback Machine also known as frameworks.
  4. ^ "int 2D". Archived from the original on 2017-12-01. Retrieved 2019-11-14.
  5. ^ "TSR Libraries". 2016-06-19. Archived from the original on 2016-06-19. Retrieved 2019-11-14.

External links