minix/minix/lib/libvassert/vassert.c

287 lines
6.1 KiB
C
Raw Normal View History

#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <setjmp.h>
#include <minix/config.h>
#include <minix/const.h>
#include <minix/ipc.h>
#include <minix/com.h>
#include <minix/syslib.h>
#include <machine/stackframe.h>
#include "vassert.h"
VAssert_StateWrapper vassert_state ALIGNED(VASSERT_PAGE_SIZE);
#define TRUE 1
#define FALSE 0
#define MAGIC_CMD 0x564d5868
#define MAGIC_PORT 0x5658
#define HIGH_BAND_PORT 0x5659
#define BACKDOOR_PORT 51
#define BACKDOOR_HB_PORT 1
#define CMD_SET_ADDRESS BACKDOOR_PORT|(1<<16)
#define CMD_RETURN_REPLAY BACKDOOR_PORT|(2<<16)
#define CMD_GO_LIVE BACKDOOR_PORT|(3<<16)
#define CMD_LOG BACKDOOR_HB_PORT|(4<<16)
#define CMD_SET_RECORD 47
#define LOG_MAX 512
typedef char Bool;
typedef unsigned int uint32;
typedef unsigned long long uint64;
#ifdef VM_X86_64
typedef uint64 VA;
#else
typedef uint32 VA;
#endif
static sigjmp_buf segv_jmp;
void libvassert_process_backdoor(uint32, uint32, uint32, reg_t *, reg_t *,
reg_t *, reg_t *);
/*
*---------------------------------------------------------------------
*
* sig_segv --
*
* Customed SEGV signal handler for VAssert_IsInVM.
*
* Results:
*
* None.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------
*/
static void __dead sig_segv(int sig_no)
{
/* jumping to error handling in VAssert_IsInVM. */
siglongjmp(segv_jmp, 1);
}
/*
*---------------------------------------------------------------------
*
* VAssert_IsInVM --
*
* Check if we are in virtual world.
*
* Results:
*
* Return TRUE on success, or FALSE on failure.
*
* Side effects:
* None.
*
*---------------------------------------------------------------------
*/
static Bool VAssert_IsInVM(void)
{
uint32 eax, ebx, ecx, edx;
static Bool inVM = FALSE;
static Bool tested = FALSE;
if (!tested) {
/* Error handling. */
if (sigsetjmp(segv_jmp, 0) != 0) {
signal(SIGSEGV, SIG_DFL);
inVM = FALSE;
return inVM;
}
tested = TRUE;
/* Install custom handler. */
signal(SIGSEGV, sig_segv);
/* Test if we are in a VM. */
libvassert_process_backdoor(0x0a, 0, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
signal(SIGSEGV, SIG_DFL);
inVM = TRUE;
}
return inVM;
}
/*
*---------------------------------------------------------------------
*
* VAssert_Init --
*
* Tell vmx that vassert is inited.
*
* Results:
*
* Return 0 on success, or -1 on failure.
*
* Side effects:
* None
*
*---------------------------------------------------------------------
*/
char VAssert_Init(void)
{
uint32 eax, ebx, ecx, edx;
No more intel/minix segments. This commit removes all traces of Minix segments (the text/data/stack memory map abstraction in the kernel) and significance of Intel segments (hardware segments like CS, DS that add offsets to all addressing before page table translation). This ultimately simplifies the memory layout and addressing and makes the same layout possible on non-Intel architectures. There are only two types of addresses in the world now: virtual and physical; even the kernel and processes have the same virtual address space. Kernel and user processes can be distinguished at a glance as processes won't use 0xF0000000 and above. No static pre-allocated memory sizes exist any more. Changes to booting: . The pre_init.c leaves the kernel and modules exactly as they were left by the bootloader in physical memory . The kernel starts running using physical addressing, loaded at a fixed location given in its linker script by the bootloader. All code and data in this phase are linked to this fixed low location. . It makes a bootstrap pagetable to map itself to a fixed high location (also in linker script) and jumps to the high address. All code and data then use this high addressing. . All code/data symbols linked at the low addresses is prefixed by an objcopy step with __k_unpaged_*, so that that code cannot reference highly-linked symbols (which aren't valid yet) or vice versa (symbols that aren't valid any more). . The two addressing modes are separated in the linker script by collecting the unpaged_*.o objects and linking them with low addresses, and linking the rest high. Some objects are linked twice, once low and once high. . The bootstrap phase passes a lot of information (e.g. free memory list, physical location of the modules, etc.) using the kinfo struct. . After this bootstrap the low-linked part is freed. . The kernel maps in VM into the bootstrap page table so that VM can begin executing. Its first job is to make page tables for all other boot processes. So VM runs before RS, and RS gets a fully dynamic, VM-managed address space. VM gets its privilege info from RS as usual but that happens after RS starts running. . Both the kernel loading VM and VM organizing boot processes happen using the libexec logic. This removes the last reason for VM to still know much about exec() and vm/exec.c is gone. Further Implementation: . All segments are based at 0 and have a 4 GB limit. . The kernel is mapped in at the top of the virtual address space so as not to constrain the user processes. . Processes do not use segments from the LDT at all; there are no segments in the LDT any more, so no LLDT is needed. . The Minix segments T/D/S are gone and so none of the user-space or in-kernel copy functions use them. The copy functions use a process endpoint of NONE to realize it's a physical address, virtual otherwise. . The umap call only makes sense to translate a virtual address to a physical address now. . Segments-related calls like newmap and alloc_segments are gone. . All segments-related translation in VM is gone (vir2map etc). . Initialization in VM is simpler as no moving around is necessary. . VM and all other boot processes can be linked wherever they wish and will be mapped in at the right location by the kernel and VM respectively. Other changes: . The multiboot code is less special: it does not use mb_print for its diagnostics any more but uses printf() as normal, saving the output into the diagnostics buffer, only printing to the screen using the direct print functions if a panic() occurs. . The multiboot code uses the flexible 'free memory map list' style to receive the list of free memory if available. . The kernel determines the memory layout of the processes to a degree: it tells VM where the kernel starts and ends and where the kernel wants the top of the process to be. VM then uses this entire range, i.e. the stack is right at the top, and mmap()ped bits of memory are placed below that downwards, and the break grows upwards. Other Consequences: . Every process gets its own page table as address spaces can't be separated any more by segments. . As all segments are 0-based, there is no distinction between virtual and linear addresses, nor between userspace and kernel addresses. . Less work is done when context switching, leading to a net performance increase. (8% faster on my machine for 'make servers'.) . The layout and configuration of the GDT makes sysenter and syscall possible.
2012-05-07 16:03:35 +02:00
VA page_address = (VA) &vassert_state.inReplay;
if (!VAssert_IsInVM()) {
return -1;
}
bzero((char*) &vassert_state, sizeof vassert_state);
#ifndef __minix
/* Lock the page. */
if (mlock(&vassert_state, sizeof vassert_state)) {
return -1;
}
#endif
No more intel/minix segments. This commit removes all traces of Minix segments (the text/data/stack memory map abstraction in the kernel) and significance of Intel segments (hardware segments like CS, DS that add offsets to all addressing before page table translation). This ultimately simplifies the memory layout and addressing and makes the same layout possible on non-Intel architectures. There are only two types of addresses in the world now: virtual and physical; even the kernel and processes have the same virtual address space. Kernel and user processes can be distinguished at a glance as processes won't use 0xF0000000 and above. No static pre-allocated memory sizes exist any more. Changes to booting: . The pre_init.c leaves the kernel and modules exactly as they were left by the bootloader in physical memory . The kernel starts running using physical addressing, loaded at a fixed location given in its linker script by the bootloader. All code and data in this phase are linked to this fixed low location. . It makes a bootstrap pagetable to map itself to a fixed high location (also in linker script) and jumps to the high address. All code and data then use this high addressing. . All code/data symbols linked at the low addresses is prefixed by an objcopy step with __k_unpaged_*, so that that code cannot reference highly-linked symbols (which aren't valid yet) or vice versa (symbols that aren't valid any more). . The two addressing modes are separated in the linker script by collecting the unpaged_*.o objects and linking them with low addresses, and linking the rest high. Some objects are linked twice, once low and once high. . The bootstrap phase passes a lot of information (e.g. free memory list, physical location of the modules, etc.) using the kinfo struct. . After this bootstrap the low-linked part is freed. . The kernel maps in VM into the bootstrap page table so that VM can begin executing. Its first job is to make page tables for all other boot processes. So VM runs before RS, and RS gets a fully dynamic, VM-managed address space. VM gets its privilege info from RS as usual but that happens after RS starts running. . Both the kernel loading VM and VM organizing boot processes happen using the libexec logic. This removes the last reason for VM to still know much about exec() and vm/exec.c is gone. Further Implementation: . All segments are based at 0 and have a 4 GB limit. . The kernel is mapped in at the top of the virtual address space so as not to constrain the user processes. . Processes do not use segments from the LDT at all; there are no segments in the LDT any more, so no LLDT is needed. . The Minix segments T/D/S are gone and so none of the user-space or in-kernel copy functions use them. The copy functions use a process endpoint of NONE to realize it's a physical address, virtual otherwise. . The umap call only makes sense to translate a virtual address to a physical address now. . Segments-related calls like newmap and alloc_segments are gone. . All segments-related translation in VM is gone (vir2map etc). . Initialization in VM is simpler as no moving around is necessary. . VM and all other boot processes can be linked wherever they wish and will be mapped in at the right location by the kernel and VM respectively. Other changes: . The multiboot code is less special: it does not use mb_print for its diagnostics any more but uses printf() as normal, saving the output into the diagnostics buffer, only printing to the screen using the direct print functions if a panic() occurs. . The multiboot code uses the flexible 'free memory map list' style to receive the list of free memory if available. . The kernel determines the memory layout of the processes to a degree: it tells VM where the kernel starts and ends and where the kernel wants the top of the process to be. VM then uses this entire range, i.e. the stack is right at the top, and mmap()ped bits of memory are placed below that downwards, and the break grows upwards. Other Consequences: . Every process gets its own page table as address spaces can't be separated any more by segments. . As all segments are 0-based, there is no distinction between virtual and linear addresses, nor between userspace and kernel addresses. . Less work is done when context switching, leading to a net performance increase. (8% faster on my machine for 'make servers'.) . The layout and configuration of the GDT makes sysenter and syscall possible.
2012-05-07 16:03:35 +02:00
libvassert_process_backdoor(CMD_SET_ADDRESS, page_address,
MAGIC_PORT|(1<<16), &eax, &ebx, &ecx, &edx);
return (eax != (uint32)-1) ? 0 : -1;
}
/*
*---------------------------------------------------------------------
*
* VAssert_Uninit --
*
* Tell vmx that vassert is finalized.
*
* Results:
*
* Return 0 on success, or -1 on failure.
*
* Side effects:
* None
*
*---------------------------------------------------------------------
*/
char VAssert_Uninit(void)
{
unsigned int eax, ebx, ecx, edx;
if (!VAssert_IsInVM()) {
return -1;
}
libvassert_process_backdoor(CMD_SET_ADDRESS, 0, MAGIC_PORT|(0<<16), &eax, &ebx, &ecx, &edx);
return (eax != (unsigned int)-1) ? 0 : 1;
}
/*
*---------------------------------------------------------------------
*
* VAssert_LogMain --
*
* Print message to a text file on host side.
*
* Results:
* None
*
* Side effects:
* Write to a text file with fixed name.
* If the file exists, host UI will ask for append/replace/ignore
*
*---------------------------------------------------------------------
*/
void VAssert_LogMain(const char *format, ...)
{
unsigned int eax, ebx, ecx, edx;
char buf[LOG_MAX];
unsigned int len = 0;
va_list ap;
va_start(ap, format);
len = vsnprintf(buf, LOG_MAX, format, ap);
va_end(ap);
__asm__ __volatile__("cld; rep outsb;"
: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
: "0"(MAGIC_CMD), "1"(CMD_LOG), "2"(len), "d"(HIGH_BAND_PORT), "S"(buf)
: "memory"
);
}
/*
*---------------------------------------------------------------------
*
* VAssert_GoLiveMain --
*
* Make the vm which is in replay exit replay.
*
* Results:
* None
*
* Side effects:
* Replay is stopped.
*
*---------------------------------------------------------------------
*/
void VAssert_GoLiveMain(void)
{
unsigned int eax, ebx, ecx, edx;
vassert_state.inReplay = 0;
libvassert_process_backdoor(CMD_GO_LIVE, 0, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
}
/*
*---------------------------------------------------------------------
*
* VAssert_ReturnToReplayMain --
*
* Called after the custom work is done, and replay is to continue.
*
* Results:
* None
*
* Side effects:
* Replay is continued from pause.
*
*---------------------------------------------------------------------
*/
void VAssert_ReturnToReplayMain(void)
{
unsigned int eax, ebx, ecx, edx;
libvassert_process_backdoor(CMD_RETURN_REPLAY, 0, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
}
/*
*---------------------------------------------------------------------
*
* VAssert_SetRecordingMain --
*
* Ask vmx for starting or stopping recording.
*
* Results:
*
* Return TRUE on success, or FALSE on failure.
*
* Side effects:
* Recording is started or stopped.
*
*---------------------------------------------------------------------
*/
char VAssert_SetRecordingMain(char start)
{
uint32 eax, ebx, ecx, edx;
if (!VAssert_IsInVM()) {
return FALSE;
}
libvassert_process_backdoor(CMD_SET_RECORD, start ? 1 : 2, MAGIC_PORT, &eax, &ebx, &ecx, &edx);
return (eax == 1) ? TRUE : FALSE;
}