⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cdosx32.c

📁 大量的汇编程序源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
 * FILE: cdosx32.c							     *
 *									     *
 * DESC:								     *
 *	- int 0x21 dos call handler					     *
 *									     *
 * Copyright (C) 1993,1994						     *
 *	Rainer Schnitker, Heeper Str. 283, 33607 Bielefeld		     *
 *	email: rainer@mathematik.uni-bielefeld.de			     *
 *									     *
 *****************************************************************************/

#include "DPMI.H"
#include "PRINTF.H"
#include "RMLIB.H"
#include "PROCESS.H"
#include "FS.H"
#include "SIGNALS.H"
#include "START32.H"
#include "CDOSX32.H"
#include "COPY32.H"
#include "TERMIO.H"
#include "DOSERRNO.H"
#include "DJIO.H"
#include "RSX.H"

/* define segment offset usage */
#ifdef __EMX__
#define SET_SEG_OFF(pointer, seg, off) \
    { seg = (((unsigned) (pointer) & ~0xFFF) >> 4) + ds16real; \
	off = (unsigned) (pointer) & 0xFFF; }

#else				/* 16 bit compiler */
#define SET_SEG_OFF(pointer, seg, off) \
    { seg = ds16real; off = (WORD) (pointer) ;}
#endif

#ifdef __EMX__
#define SET_SEG_OFF_64(pointer, seg, off) \
    { seg = (((unsigned) (pointer) & ~0xFFFF) >> 4) + ds16real; \
	off = (unsigned) (pointer) & 0xFFFF; }

#else				/* 16 bit compiler */
#define SET_SEG_OFF_64(pointer, seg, off) \
    { seg = ds16real; off = (WORD) (pointer) ;}
#endif

extern WORD _psp;		/* PSP after protected mode switch */

static char iobuffer[IOBUF_SIZE + 3];
char *iobuf = iobuffer; 	/* I/O buffer for real-mode operations */
DWORD iobuf_addr;

DWORD iobuf_linear;		/* linear address I/O for djgpp */

void align_iobuf(void)
{
    iobuf = (char *) ((unsigned) (iobuf + 3) & ~3);
    iobuf_addr = ((DWORD) ds16real << 4) + (DWORD) (UINT) iobuf;
    iobuf_linear = iobuf_addr + 4096L;	/* if we use this buffer */

    if (opt_printall)
	printf("lin address of iobuf %lX\n", iobuf_linear);
}


#define CONVERT_BX_DOSHANDLE {	\
    int h = get_dos_handle(BX); \
    if (h < 0) {		\
	EAX = EMX_EBADF;	\
	return CARRY_ON;	\
    }				\
    else BX = h; }


static void put_regs(TRANSLATION *tr)
{
    tr->eax = EAX;
    tr->ebx = EBX;
    tr->ecx = ECX;
    tr->edx = EDX;
}

static void get_regs(TRANSLATION *tr)
{
    EAX = tr->eax;
    EBX = tr->ebx;
    ECX = tr->ecx;
    EDX = tr->edx;
    FLAGS = tr->flags;
}

static char rm_stack[512];
unsigned real_mode_stack = (unsigned) (rm_stack + 510);

/* call Real-Mode INT0x21 */
static int realdos(TRANSLATION *tr)
{
    SET_SEG_OFF_64(real_mode_stack, tr->ss, tr->sp);

    tr->flags = 0x3200;
    tr->eax &= 0xFFFF;
    tr->cs = cs16real;
    tr->es = tr->ds;

    /* DPMI-Call to Real-Mode DOS */
    SimulateRMint(0x21, 0, 0, tr);

    /* return Carry-bit */
    return (tr->flags & 1);
}

static int illegal_syscall()
{
    puts("Illegal Parameter in syscall");
    send_signal(npz, SIGSEGV);
    EAX = EMX_EINTR;
    return CARRY_ON;
}

#define TEST_ILLEGAL( OFFSET, LENGHT ) \
if (verify_illegal(npz, OFFSET, LENGHT)) return illegal_syscall();

#define TEST_ILLEGAL_WRITE( OFFSET, LENGHT ) \
if (verify_illegal_write(npz, OFFSET, LENGHT)) return illegal_syscall();

/*
** INT 0x21 handler returns:
**
** CARRY_ON :	error, set carry-bit, errno in eax
** CARRY_OFF:	no error, clear carry-bit
** CARRY_NON:	carry-bit set by realdos -> dos error code in ax,
**		translate ax to errno
*/
int int21normal(void)
{
    static DWORD user_dta;	/* prot-mode DTA address */
    TRANSLATION tr;		/* registers set for mode-switching */
    int err, new_handle;
    unsigned char rAH, rAL;

    rAH = (BYTE) (AX >> 8);
    rAL = (BYTE) EAX;

    switch (rAH) {
	/*
	** register based functions
	** no changes needed - only ax,dx,flags (bx,cx) used
	*/
    case 0x01:			/* read char */
    case 0x02:			/* write char */
    case 0x03:			/* read char stdaux */
    case 0x04:			/* write char stdaux */
    case 0x05:			/* read char prn */
    case 0x06:			/* con output&input */
    case 0x07:			/* char input & echo */
    case 0x08:			/* char output & echo */
    case 0x0b:			/* get stdin stat */
    case 0x0c:			/* flush buffer & read std input */
    case 0x0d:			/* disk reset */
    case 0x0e:			/* select drive */
    case 0x19:			/* get drive */
    case 0x2a:			/* get system date (cx) */
    case 0x2b:			/* set system date (cx) */
    case 0x2c:			/* get time (cx) */
    case 0x2d:			/* set time (cx) */
    case 0x2e:			/* set verify */
    case 0x30:			/* get DOS version (bx,cx) */
    case 0x33:			/* extended break check */
    case 0x36:			/* get free disk space (bx,cx) */
    case 0x37:			/* get&set switch char */
    case 0x54:			/* get verify flag */
    case 0x58:			/* get/set UMB link state */
    case 0x5c:			/* un-&lock file */
    case 0x66:			/* code page */
	put_regs(&tr);
	realdos(&tr);
	get_regs(&tr);
	return CARRY_NON;
    case 0x44:			/* IOCTL */
	switch (rAL) {
	case 0x00:		/* get device info */
	case 0x01:		/* set device info */
	case 0x06:		/* get input status */
	case 0x07:		/* get output status */
	case 0x0a:		/* check handle remote */
	    CONVERT_BX_DOSHANDLE;
	    /* fall through */
	case 0x08:		/* check block device remove */
	case 0x09:		/* check block device remote */
	case 0x0b:		/* set sharing */
	case 0x0e:		/* get log drive map */
	case 0x0f:		/* set log drive map */
	case 0x10:		/* query ioctl handle */
	case 0x11:		/* query ioctl drive */
	    put_regs(&tr);
	    realdos(&tr);
	    get_regs(&tr);
	    return CARRY_NON;
	default:
	    EAX = EMX_EIO;
	    return CARRY_ON;
	}

	/*
	** simple file handle functions
	** convert handle
	*/
    case 0x57:			/* get file state (bx,cx) */
    case 0x68:			/* fflush file (bx) */
	CONVERT_BX_DOSHANDLE;
	put_regs(&tr);
	realdos(&tr);
	get_regs(&tr);
	return CARRY_NON;

    case 0x3e:			/* close file (bx) */
	err = sys_close(BX);
	if (err < 0) {
	    EAX = emx_errno;
	    return CARRY_ON;
	} else {
	    EAX = 0;
	    return CARRY_OFF;
	}

    case 0x42:			/* lseek file */
	{
	    long pos;

	    if (npz->p_flags & PF_EMX_FILE)
		pos = EDX;
	    else
		pos = (ECX << 16) | (EDX & 0xFFFF);

	    if ((EAX = sys_lseek(BX, pos, rAL))== -1L) {
		EAX = emx_errno;
		return CARRY_ON;
	    }
	    if (npz->p_flags & PF_DJGPP_FILE) {
		EDX = EAX >> 16;
	    }
	    return CARRY_OFF;
	}

    case 0x45:			/* dup file (bx) */
	if ((new_handle = sys_dup(BX)) < 0) {
	    EAX = (long) -new_handle;
	    return CARRY_ON;
	}
	EAX = (long) new_handle;
	return CARRY_OFF;

    case 0x46:			/* dup2 file (bx,cx) */
	if ((new_handle = sys_dup2(BX, CX)) < 0) {
	    EAX = (long) -new_handle;
	    return CARRY_ON;
	}
	EAX = (long) new_handle;
	return CARRY_OFF;

	/*
	** ASCiiZero functions
	**
	** copy name -> real_buffer , call realdos
	*/

    case 0x09:			/* string to output */
	TEST_ILLEGAL(EDX, 2);
	getstr32_16(DS, EDX, iobuf, '$');
	tr.eax = EAX;
	SET_SEG_OFF(iobuf, tr.ds, tr.edx);
	realdos(&tr);
	return CARRY_NON;

    case 0x39:			/* MKDIR name */
    case 0x3a:			/* RMDIR name */
    case 0x3b:			/* CHDIR name */
    case 0x41:			/* UNLINK name */
    case 0x43:			/* ATTRIB name */
	TEST_ILLEGAL(EDX, 2);
	if (rAH == 0x3c)	/* fix umask bug */
	    ECX &= ~1L;
	put_regs(&tr);
	strcpy32_16(DS, EDX, iobuf);
	SET_SEG_OFF(iobuf, tr.ds, tr.edx);
	realdos(&tr);
	get_regs(&tr);
	return CARRY_NON;

    case 0x3c:			/* CREATE name */
    case 0x3d:			/* OPEN name */
    case 0x5b:			/* CREATE New file */
	/*
	** only DOS handles !!
	** others with EMX: 0x7F ; DJGPP 0xFF
	*/
	if (rAH == 0x3c)	/* fix umask bug */
	    ECX &= ~1L;
	TEST_ILLEGAL(EDX, 2);
	strcpy32_16(DS, EDX, iobuf);
	put_regs(&tr);
	SET_SEG_OFF(iobuf, tr.ds, tr.edx);
	if (!realdos(&tr)) {
	    new_handle = get_empty_proc_filp();
	    npz->filp[new_handle]->f_mode = FMODE_READ | FMODE_WRITE;
	    npz->filp[new_handle]->f_doshandle = (BYTE) tr.eax;
	    npz->filp[new_handle]->f_op = & msdos_fop;
	    tr.eax = (long) new_handle;
	}
	get_regs(&tr);
	return CARRY_NON;

    case 0x56:			/* RENAME oldname name (strlen < 1024) */
	TEST_ILLEGAL(EDX, 2);
	TEST_ILLEGAL(EDI, 2);
	strcpy32_16(DS, EDX, iobuf);
	strcpy32_16(DS, EDI, iobuf + 1024);
	put_regs(&tr);
	SET_SEG_OFF(iobuf, tr.ds, tr.edx);
	tr.es = tr.ds;
	tr.edi = tr.edx + 1024;
	realdos(&tr);
	get_regs(&tr);
	return CARRY_NON;

    case 0x5a:			/* CREATE TEMP name */
	TEST_ILLEGAL_WRITE(EDX, 2);
	put_regs(&tr);
	strcpy32_16(DS, EDX, iobuf);
	SET_SEG_OFF(iobuf, tr.ds, tr.edx);
	if (!realdos(&tr)) {
	    strcpy16_32(DS, EDX, iobuf);
	    new_handle = get_empty_proc_filp();
	    npz->filp[new_handle]->f_mode = FMODE_READ | FMODE_WRITE;
	    npz->filp[new_handle]->f_doshandle = (BYTE) tr.eax;
	    npz->filp[new_handle]->f_op = & msdos_fop;
	    tr.eax = (long) new_handle;
	}
	get_regs(&tr);
	return CARRY_NON;

    case 0x6c:			/* EXTENDED OPEN/CREATE file */
	TEST_ILLEGAL(ESI, 2);
	strcpy32_16(DS, ESI, iobuf);
	put_regs(&tr);
	SET_SEG_OFF(iobuf, tr.ds, tr.esi);
	if (!realdos(&tr)) {
	    new_handle = get_empty_proc_filp();
	    npz->filp[new_handle]->f_mode = FMODE_READ | FMODE_WRITE;
	    npz->filp[new_handle]->f_doshandle = (BYTE) tr.eax;
	    npz->filp[new_handle]->f_op = & msdos_fop;
	    tr.eax = (long) new_handle;
	}
	get_regs(&tr);
	return CARRY_NON;

	/*
	** buffer functions
	**
	** copy data before or after realdos
	*/
    case 0x0a:			/* BUFFERED INPUT */
	{
	    BYTE no_chars;

	    TEST_ILLEGAL_WRITE(EDX, 4);
	    put_regs(&tr);
	    no_chars = (BYTE) read32(DS, EDX);
	    iobuf[0] = no_chars;/* no of chars */
	    SET_SEG_OFF(iobuf, tr.ds, tr.edx);
	    realdos(&tr);
	    cpy16_32(DS, EDX, iobuf, (DWORD) no_chars + 2);	/* copy of chars */
	    return CARRY_OFF;
	}

    case 0x3f:			/* READ from file */
	if (EDX != iobuf_linear) {
	    TEST_ILLEGAL(EDX, ECX);
	}
	if (ECX == 0) {
	    EAX = 0;
	    return CARRY_OFF;
	}
	if ((npz->p_flags & PF_DJGPP_FILE) && EDX == 0) {
	    printf("read.o not ok ; run DPMIFIX from DJGPP 1.10\n");
	    EAX = EMX_EIO;
	    return CARRY_ON;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -