码迷,mamicode.com
首页 > 其他好文 > 详细

哈工大操作系统实验5—I/O设备管理

时间:2016-04-26 00:38:27      阅读:429      评论:0      收藏:0      [点我收藏+]

标签:

实验内容

本实验的基本内容是修改Linux 0.11的终端设备处理代码,对键盘输入和字符显示进行非常规的控制。

在初始状态,一切如常。用户按一次F12后,把应用程序向终端输出所有字母都替换为“*”。用户再按一次F12,又恢复正常。第三次按F12,再进行输出替换。依此类推。

以ls命令为例:

正常情况:

ls 
hello.c hello.o hello

第一次按F12,然后输入ls:

** 
*

第二次按F12,然后输入ls:

ls 
hello.c hello.o hello

第三次按F12,然后输入ls:

** 
*

linux-0.11/kernel/chr_drv/keyboard.S

func:
    /*程序210行子函数处理功能键
      将功能键转化成转义字符存取到读队列中*/
    pushl %eax
    pushl %ecx
    pushl %edx
    //call show_stat   //将该行注释掉,调用显示各任务的状态函数,我们不需要显示
    popl %edx
    popl %ecx
    popl %eax

linux-0.11/kernel/chr_drv/tty_io.c

修改copy_to_cooked函数 

增加字符判断,由于按下F12时,判断是否为F1-F12的扫描码,若是,则将查func_table中的4个字节的转义字符序列放入缓冲队列中。 
若不是,则不处理并返回。例如:功能键发送的扫描码,F12键为:‘esc [ [ L’。

具体参考:Linux0.11完全注释keyboard.S文件238行左右的代码。 
/* 
* linux/kernel/tty_io.c 
* 
* (C) 1991 Linus Torvalds 
*/

/*
 * 'tty_io.c' gives an orthogonal feeling to tty's, be they consoles
 * or rs-channels. It also implements echoing, cooked mode etc.
 *
 * Kill-line thanks to John T Kohl.
 */
#include <ctype.h>
#include <errno.h>
#include <signal.h>

#define ALRMMASK (1<<(SIGALRM-1))
#define KILLMASK (1<<(SIGKILL-1))
#define INTMASK (1<<(SIGINT-1))
#define QUITMASK (1<<(SIGQUIT-1))
#define TSTPMASK (1<<(SIGTSTP-1))

#include <linux/sched.h>
#include <linux/tty.h>
#include <asm/segment.h>
#include <asm/system.h>

#define _L_FLAG(tty,f)  ((tty)->termios.c_lflag & f)
#define _I_FLAG(tty,f)  ((tty)->termios.c_iflag & f)
#define _O_FLAG(tty,f)  ((tty)->termios.c_oflag & f)

#define L_CANON(tty)    _L_FLAG((tty),ICANON)
#define L_ISIG(tty) _L_FLAG((tty),ISIG)
#define L_ECHO(tty) _L_FLAG((tty),ECHO)
#define L_ECHOE(tty)    _L_FLAG((tty),ECHOE)
#define L_ECHOK(tty)    _L_FLAG((tty),ECHOK)
#define L_ECHOCTL(tty)  _L_FLAG((tty),ECHOCTL)
#define L_ECHOKE(tty)   _L_FLAG((tty),ECHOKE)

#define I_UCLC(tty) _I_FLAG((tty),IUCLC)
#define I_NLCR(tty) _I_FLAG((tty),INLCR)
#define I_CRNL(tty) _I_FLAG((tty),ICRNL)
#define I_NOCR(tty) _I_FLAG((tty),IGNCR)

#define O_POST(tty) _O_FLAG((tty),OPOST)
#define O_NLCR(tty) _O_FLAG((tty),ONLCR)
#define O_CRNL(tty) _O_FLAG((tty),OCRNL)
#define O_NLRET(tty)    _O_FLAG((tty),ONLRET)
#define O_LCUC(tty) _O_FLAG((tty),OLCUC)


int flag = -1;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
int flag4 = 0;


struct tty_struct tty_table[] = {
    {
        {ICRNL,     /* change incoming CR to NL */
        OPOST|ONLCR,    /* change outgoing NL to CRNL */
        0,
        ISIG | ICANON | ECHO | ECHOCTL | ECHOKE,
        0,      /* console termio */
        INIT_C_CC},
        0,          /* initial pgrp */
        0,          /* initial stopped */
        con_write,
        {0,0,0,0,""},       /* console read-queue */
        {0,0,0,0,""},       /* console write-queue */
        {0,0,0,0,""}        /* console secondary queue */
    },{
        {0, /* no translation */
        0,  /* no translation */
        B2400 | CS8,
        0,
        0,
        INIT_C_CC},
        0,
        0,
        rs_write,
        {0x3f8,0,0,0,""},       /* rs 1 */
        {0x3f8,0,0,0,""},
        {0,0,0,0,""}
    },{
        {0, /* no translation */
        0,  /* no translation */
        B2400 | CS8,
        0,
        0,
        INIT_C_CC},
        0,
        0,
        rs_write,
        {0x2f8,0,0,0,""},       /* rs 2 */
        {0x2f8,0,0,0,""},
        {0,0,0,0,""}
    }
};

/*
 * these are the tables used by the machine code handlers.
 * you can implement pseudo-tty's or something by changing
 * them. Currently not done.
 */
struct tty_queue * table_list[]={
    &tty_table[0].read_q, &tty_table[0].write_q,
    &tty_table[1].read_q, &tty_table[1].write_q,
    &tty_table[2].read_q, &tty_table[2].write_q
    };

void tty_init(void)
{
    rs_init();
    con_init();
}

void tty_intr(struct tty_struct * tty, int mask)
{
    int i;

    if (tty->pgrp <= 0)
        return;
    for (i=0;i<NR_TASKS;i++)
        if (task[i] && task[i]->pgrp==tty->pgrp)
            task[i]->signal |= mask;
}

static void sleep_if_empty(struct tty_queue * queue)
{
    cli();
    while (!current->signal && EMPTY(*queue))
        interruptible_sleep_on(&queue->proc_list);
    sti();
}

static void sleep_if_full(struct tty_queue * queue)
{
    if (!FULL(*queue))
        return;
    cli();
    while (!current->signal && LEFT(*queue)<128)
        interruptible_sleep_on(&queue->proc_list);
    sti();
}

void wait_for_keypress(void)
{
    sleep_if_empty(&tty_table[0].secondary);
}


void copy_to_cooked(struct tty_struct * tty)
{
    signed char c;

    while (!EMPTY(tty->read_q) && !FULL(tty->secondary)) {
        GETCH(tty->read_q,c);
        /*printk("\nI am %d\n",c);*/

        if (c == 27)
        {
            flag1 = 1;
        }
        else if (flag1 == 1 && c == 91 && flag2 == 0)
        {
            flag2 = 1;
        }
        else if (flag1 == 1 && flag2 == 1 && c == 91)
        {
            flag3 = 1;
        }
        else if (flag1 == 1 && flag2 == 1 && flag3 == 1 && c == 'L')
        {
            flag *= -1;
            continue ;
        }
        else 
        {
            flag1 = 0;
            flag2 = 0;
            flag3 = 0;
        }

        if (c==13)
            if (I_CRNL(tty))
                c=10;
            else if (I_NOCR(tty))
                continue;
            else ;
        else if (c==10 && I_NLCR(tty))
            c=13;
        if (I_UCLC(tty))
            c=tolower(c);
        if (L_CANON(tty)) {
            if (c==KILL_CHAR(tty)) {
                /* deal with killing the input line */
                while(!(EMPTY(tty->secondary) ||
                (c=LAST(tty->secondary))==10 ||
                c==EOF_CHAR(tty))) {
                    if (L_ECHO(tty)) {
                        if (c<32)
                            PUTCH(127,tty->write_q);
                        PUTCH(127,tty->write_q);
                        tty->write(tty);
                    }
                    DEC(tty->secondary.head);
                }
                continue;
            }
            if (c==ERASE_CHAR(tty)) {
                if (EMPTY(tty->secondary) ||
                   (c=LAST(tty->secondary))==10 ||
                   c==EOF_CHAR(tty))
                    continue;
                if (L_ECHO(tty)) {
                    if (c<32)
                        PUTCH(127,tty->write_q);
                    PUTCH(127,tty->write_q);
                    tty->write(tty);
                }
                DEC(tty->secondary.head);
                continue;
            }
            if (c==STOP_CHAR(tty)) {
                tty->stopped=1;
                continue;
            }
            if (c==START_CHAR(tty)) {
                tty->stopped=0;
                continue;
            }
        }
        if (L_ISIG(tty)) {
            if (c==INTR_CHAR(tty)) {
                tty_intr(tty,INTMASK);
                continue;
            }
            if (c==QUIT_CHAR(tty)) {
                tty_intr(tty,QUITMASK);
                continue;
            }
        }
        if (c==10 || c==EOF_CHAR(tty))
            tty->secondary.data++;
        if (L_ECHO(tty)) {
            if (c==10) {
                PUTCH(10,tty->write_q);
                PUTCH(13,tty->write_q);
            } else if (c<32) {
                if (L_ECHOCTL(tty)) {
                    PUTCH('^',tty->write_q);
                    PUTCH(c+64,tty->write_q);
                }
            } else
                PUTCH(c,tty->write_q);
            tty->write(tty);
        }
        /*printk("\nI am %d after..\n",c);*/
        PUTCH(c,tty->secondary);
    }
    wake_up(&tty->secondary.proc_list);
}

int tty_read(unsigned channel, char * buf, int nr)
{
    struct tty_struct * tty;
    char c, * b=buf;
    int minimum,time,flag=0;
    long oldalarm;

    if (channel>2 || nr<0) return -1;
    tty = &tty_table[channel];
    oldalarm = current->alarm;
    time = 10L*tty->termios.c_cc[VTIME];
    minimum = tty->termios.c_cc[VMIN];
    if (time && !minimum) {
        minimum=1;
        if ((flag=(!oldalarm || time+jiffies<oldalarm)))
            current->alarm = time+jiffies;
    }
    if (minimum>nr)
        minimum=nr;
    while (nr>0) {
        if (flag && (current->signal & ALRMMASK)) {
            current->signal &= ~ALRMMASK;
            break;
        }
        if (current->signal)
            break;
        if (EMPTY(tty->secondary) || (L_CANON(tty) &&
        !tty->secondary.data && LEFT(tty->secondary)>20)) {
            sleep_if_empty(&tty->secondary);
            continue;
        }
        do {
            GETCH(tty->secondary,c);
            if (c==EOF_CHAR(tty) || c==10)
                tty->secondary.data--;
            if (c==EOF_CHAR(tty) && L_CANON(tty))
                return (b-buf);
            else {
                put_fs_byte(c,b++);
                if (!--nr)
                    break;
            }
        } while (nr>0 && !EMPTY(tty->secondary));
        if (time && !L_CANON(tty)) {
            if ((flag=(!oldalarm || time+jiffies<oldalarm)))
                current->alarm = time+jiffies;
            else
                current->alarm = oldalarm;
        }
        if (L_CANON(tty)) {
            if (b-buf)
                break;
        } else if (b-buf >= minimum)
            break;
    }
    current->alarm = oldalarm;
    if (current->signal && !(b-buf))
        return -EINTR;
    return (b-buf);
}

int tty_write(unsigned channel, char * buf, int nr)
{
    static int cr_flag=0;
    struct tty_struct * tty;
    char c, *b=buf;

    if (channel>2 || nr<0) return -1;
    tty = channel + tty_table;
    while (nr>0) {
        sleep_if_full(&tty->write_q);
        if (current->signal)
            break;
        while (nr>0 && !FULL(tty->write_q)) {
            c=get_fs_byte(b);
            if (O_POST(tty)) {
                if (c=='\r' && O_CRNL(tty))
                    c='\n';
                else if (c=='\n' && O_NLRET(tty))
                    c='\r';
                if (c=='\n' && !cr_flag && O_NLCR(tty)) {
                    cr_flag = 1;
                    PUTCH(13,tty->write_q);
                    continue;
                }
                if (O_LCUC(tty))
                    c=toupper(c);
            }
            b++; nr--;
            cr_flag = 0;
            PUTCH(c,tty->write_q);
        }
        tty->write(tty);
        if (nr>0)
            schedule();
    }
    return (b-buf);
}

/*
 * Jeh, sometimes I really like the 386.
 * This routine is called from an interrupt,
 * and there should be absolutely no problem
 * with sleeping even in an interrupt (I hope).
 * Of course, if somebody proves me wrong, I'll
 * hate intel for all time :-). We'll have to
 * be careful and see to reinstating the interrupt
 * chips before calling this, though.
 *
 * I don't think we sleep here under normal circumstances
 * anyway, which is good, as the task sleeping might be
 * totally innocent.
 */
void do_tty_interrupt(int tty)
{
    copy_to_cooked(tty_table+tty);
}

void chr_dev_init(void)
{
}


linux-0.11/kernel/chr_drv/console.c

修改con_write函数,即改变显示的字符 
/* 
* linux/kernel/console.c 

* (C) 1991 Linus Torvalds 
*/

/*
 *  console.c
 *
 * This module implements the console io functions
 *  'void con_init(void)'
 *  'void con_write(struct tty_queue * queue)'
 * Hopefully this will be a rather complete VT102 implementation.
 *
 * Beeping thanks to John T Kohl.
 */

/*
 *  NOTE!!! We sometimes disable and enable interrupts for a short while
 * (to put a word in video IO), but this will work even for keyboard
 * interrupts. We know interrupts aren't enabled when getting a keyboard
 * interrupt, as we use trap-gates. Hopefully all is well.
 */

/*
 * Code to check for different video-cards mostly by Galen Hunt,
 * <g-hunt@ee.utah.edu>
 */

#include <linux/sched.h>
#include <linux/tty.h>
#include <asm/io.h>
#include <asm/system.h>

/*
 * These are set up by the setup-routine at boot-time:
 */

#define ORIG_X          (*(unsigned char *)0x90000)
#define ORIG_Y          (*(unsigned char *)0x90001)
#define ORIG_VIDEO_PAGE     (*(unsigned short *)0x90004)
#define ORIG_VIDEO_MODE     ((*(unsigned short *)0x90006) & 0xff)
#define ORIG_VIDEO_COLS     (((*(unsigned short *)0x90006) & 0xff00) >> 8)
#define ORIG_VIDEO_LINES    (25)
#define ORIG_VIDEO_EGA_AX   (*(unsigned short *)0x90008)
#define ORIG_VIDEO_EGA_BX   (*(unsigned short *)0x9000a)
#define ORIG_VIDEO_EGA_CX   (*(unsigned short *)0x9000c)

#define VIDEO_TYPE_MDA      0x10    /* Monochrome Text Display  */
#define VIDEO_TYPE_CGA      0x11    /* CGA Display          */
#define VIDEO_TYPE_EGAM     0x20    /* EGA/VGA in Monochrome Mode   */
#define VIDEO_TYPE_EGAC     0x21    /* EGA/VGA in Color Mode    */

#define NPAR 16

extern void keyboard_interrupt(void);

static unsigned char    video_type;     /* Type of display being used   */
static unsigned long    video_num_columns;  /* Number of text columns   */
static unsigned long    video_size_row;     /* Bytes per row        */
static unsigned long    video_num_lines;    /* Number of test lines     */
static unsigned char    video_page;     /* Initial video page       */
static unsigned long    video_mem_start;    /* Start of video RAM       */
static unsigned long    video_mem_end;      /* End of video RAM (sort of)   */
static unsigned short   video_port_reg;     /* Video register select port   */
static unsigned short   video_port_val;     /* Video register value port    */
static unsigned short   video_erase_char;   /* Char+Attrib to erase with    */

static unsigned long    origin;     /* Used for EGA/VGA fast scroll */
static unsigned long    scr_end;    /* Used for EGA/VGA fast scroll */
static unsigned long    pos;
static unsigned long    x,y;
static unsigned long    top,bottom;
static unsigned long    state=0;
static unsigned long    npar,par[NPAR];
static unsigned long    ques=0;
static unsigned char    attr=0x07;

static void sysbeep(void);
extern int flag;
/*
 * this is what the terminal answers to a ESC-Z or csi0c
 * query (= vt100 response).
 */
#define RESPONSE "\033[?1;2c"

/* NOTE! gotoxy thinks x==video_num_columns is ok */
static inline void gotoxy(unsigned int new_x,unsigned int new_y)
{
    if (new_x > video_num_columns || new_y >= video_num_lines)
        return;
    x=new_x;
    y=new_y;
    pos=origin + y*video_size_row + (x<<1);
}

static inline void set_origin(void)
{
    cli();
    outb_p(12, video_port_reg);
    outb_p(0xff&((origin-video_mem_start)>>9), video_port_val);
    outb_p(13, video_port_reg);
    outb_p(0xff&((origin-video_mem_start)>>1), video_port_val);
    sti();
}

static void scrup(void)
{
    if (video_type == VIDEO_TYPE_EGAC || video_type == VIDEO_TYPE_EGAM)
    {
        if (!top && bottom == video_num_lines) {
            origin += video_size_row;
            pos += video_size_row;
            scr_end += video_size_row;
            if (scr_end > video_mem_end) {
                __asm__("cld\n\t"
                    "rep\n\t"
                    "movsl\n\t"
                    "movl video_num_columns,%1\n\t"
                    "rep\n\t"
                    "stosw"
                    ::"a" (video_erase_char),
                    "c" ((video_num_lines-1)*video_num_columns>>1),
                    "D" (video_mem_start),
                    "S" (origin)
                    );
                scr_end -= origin-video_mem_start;
                pos -= origin-video_mem_start;
                origin = video_mem_start;
            } else {
                __asm__("cld\n\t"
                    "rep\n\t"
                    "stosw"
                    ::"a" (video_erase_char),
                    "c" (video_num_columns),
                    "D" (scr_end-video_size_row)
                    );
            }
            set_origin();
        } else {
            __asm__("cld\n\t"
                "rep\n\t"
                "movsl\n\t"
                "movl video_num_columns,%%ecx\n\t"
                "rep\n\t"
                "stosw"
                ::"a" (video_erase_char),
                "c" ((bottom-top-1)*video_num_columns>>1),
                "D" (origin+video_size_row*top),
                "S" (origin+video_size_row*(top+1))
                );
        }
    }
    else        /* Not EGA/VGA */
    {
        __asm__("cld\n\t"
            "rep\n\t"
            "movsl\n\t"
            "movl video_num_columns,%%ecx\n\t"
            "rep\n\t"
            "stosw"
            ::"a" (video_erase_char),
            "c" ((bottom-top-1)*video_num_columns>>1),
            "D" (origin+video_size_row*top),
            "S" (origin+video_size_row*(top+1))
            );
    }
}

static void scrdown(void)
{
    if (video_type == VIDEO_TYPE_EGAC || video_type == VIDEO_TYPE_EGAM)
    {
        __asm__("std\n\t"
            "rep\n\t"
            "movsl\n\t"
            "addl $2,%%edi\n\t"    /* %edi has been decremented by 4 */
            "movl video_num_columns,%%ecx\n\t"
            "rep\n\t"
            "stosw"
            ::"a" (video_erase_char),
            "c" ((bottom-top-1)*video_num_columns>>1),
            "D" (origin+video_size_row*bottom-4),
            "S" (origin+video_size_row*(bottom-1)-4)
            );
    }
    else        /* Not EGA/VGA */
    {
        __asm__("std\n\t"
            "rep\n\t"
            "movsl\n\t"
            "addl $2,%%edi\n\t"    /* %edi has been decremented by 4 */
            "movl video_num_columns,%%ecx\n\t"
            "rep\n\t"
            "stosw"
            ::"a" (video_erase_char),
            "c" ((bottom-top-1)*video_num_columns>>1),
            "D" (origin+video_size_row*bottom-4),
            "S" (origin+video_size_row*(bottom-1)-4)
            );
    }
}

static void lf(void)
{
    if (y+1<bottom) {
        y++;
        pos += video_size_row;
        return;
    }
    scrup();
}

static void ri(void)
{
    if (y>top) {
        y--;
        pos -= video_size_row;
        return;
    }
    scrdown();
}

static void cr(void)
{
    pos -= x<<1;
    x=0;
}

static void del(void)
{
    if (x) {
        pos -= 2;
        x--;
        *(unsigned short *)pos = video_erase_char;
    }
}

static void csi_J(int par)
{
    long count;
    long start;

    switch (par) {
        case 0: /* erase from cursor to end of display */
            count = (scr_end-pos)>>1;
            start = pos;
            break;
        case 1: /* erase from start to cursor */
            count = (pos-origin)>>1;
            start = origin;
            break;
        case 2: /* erase whole display */
            count = video_num_columns * video_num_lines;
            start = origin;
            break;
        default:
            return;
    }
    __asm__("cld\n\t"
        "rep\n\t"
        "stosw\n\t"
        ::"c" (count),
        "D" (start),"a" (video_erase_char)
        );
}

static void csi_K(int par)
{
    long count;
    long start;

    switch (par) {
        case 0: /* erase from cursor to end of line */
            if (x>=video_num_columns)
                return;
            count = video_num_columns-x;
            start = pos;
            break;
        case 1: /* erase from start of line to cursor */
            start = pos - (x<<1);
            count = (x<video_num_columns)?x:video_num_columns;
            break;
        case 2: /* erase whole line */
            start = pos - (x<<1);
            count = video_num_columns;
            break;
        default:
            return;
    }
    __asm__("cld\n\t"
        "rep\n\t"
        "stosw\n\t"
        ::"c" (count),
        "D" (start),"a" (video_erase_char)
        );
}

void csi_m(void)
{
    int i;

    for (i=0;i<=npar;i++)
        switch (par[i]) {
            case 0:attr=0x07;break;
            case 1:attr=0x0f;break;
            case 4:attr=0x0f;break;
            case 7:attr=0x70;break;
            case 27:attr=0x07;break;
        }
}

static inline void set_cursor(void)
{
    cli();
    outb_p(14, video_port_reg);
    outb_p(0xff&((pos-video_mem_start)>>9), video_port_val);
    outb_p(15, video_port_reg);
    outb_p(0xff&((pos-video_mem_start)>>1), video_port_val);
    sti();
}

static void respond(struct tty_struct * tty)
{
    char * p = RESPONSE;

    cli();
    while (*p) {
        PUTCH(*p,tty->read_q);
        p++;
    }
    sti();
    copy_to_cooked(tty);
}

static void insert_char(void)
{
    int i=x;
    unsigned short tmp, old = video_erase_char;
    unsigned short * p = (unsigned short *) pos;

    while (i++<video_num_columns) {
        tmp=*p;
        *p=old;
        old=tmp;
        p++;
    }
}

static void insert_line(void)
{
    int oldtop,oldbottom;

    oldtop=top;
    oldbottom=bottom;
    top=y;
    bottom = video_num_lines;
    scrdown();
    top=oldtop;
    bottom=oldbottom;
}

static void delete_char(void)
{
    int i;
    unsigned short * p = (unsigned short *) pos;

    if (x>=video_num_columns)
        return;
    i = x;
    while (++i < video_num_columns) {
        *p = *(p+1);
        p++;
    }
    *p = video_erase_char;
}

static void delete_line(void)
{
    int oldtop,oldbottom;

    oldtop=top;
    oldbottom=bottom;
    top=y;
    bottom = video_num_lines;
    scrup();
    top=oldtop;
    bottom=oldbottom;
}

static void csi_at(unsigned int nr)
{
    if (nr > video_num_columns)
        nr = video_num_columns;
    else if (!nr)
        nr = 1;
    while (nr--)
        insert_char();
}

static void csi_L(unsigned int nr)
{
    if (nr > video_num_lines)
        nr = video_num_lines;
    else if (!nr)
        nr = 1;
    while (nr--)
        insert_line();
}

static void csi_P(unsigned int nr)
{
    if (nr > video_num_columns)
        nr = video_num_columns;
    else if (!nr)
        nr = 1;
    while (nr--)
        delete_char();
}

static void csi_M(unsigned int nr)
{
    if (nr > video_num_lines)
        nr = video_num_lines;
    else if (!nr)
        nr=1;
    while (nr--)
        delete_line();
}

static int saved_x=0;
static int saved_y=0;

static void save_cur(void)
{
    saved_x=x;
    saved_y=y;
}

static void restore_cur(void)
{
    gotoxy(saved_x, saved_y);
}

void con_write(struct tty_struct * tty)
{
    int nr;
    char c;
    nr = CHARS(tty->write_q);
    while (nr--) {
        GETCH(tty->write_q,c);

        switch(state) {
            case 0:
                if (c>31 && c<127) {
                    if (x>=video_num_columns) {
                        x -= video_num_columns;
                        pos -= video_size_row;
                        lf();
                    }
                    if (((c>='A' && c<='Z')||(c>='a' && c<='z')) && flag == 1)
                        c = '*';
                    __asm__("movb attr,%%ah\n\t"
                        "movw %%ax,%1\n\t"
                        ::"a" (c),"m" (*(short *)pos)
                        );
                    pos += 2;
                    x++;
                } else if (c==27)
                    state=1;
                else if (c==10 || c==11 || c==12)
                    lf();
                else if (c==13)
                    cr();
                else if (c==ERASE_CHAR(tty))
                    del();
                else if (c==8) {
                    if (x) {
                        x--;
                        pos -= 2;
                    }
                } else if (c==9) {
                    c=8-(x&7);
                    x += c;
                    pos += c<<1;
                    if (x>video_num_columns) {
                        x -= video_num_columns;
                        pos -= video_size_row;
                        lf();
                    }
                    c=9;
                } else if (c==7)
                    sysbeep();
                break;
            case 1:
                state=0;
                if (c=='[')
                    state=2;
                else if (c=='E')
                    gotoxy(0,y+1);
                else if (c=='M')
                    ri();
                else if (c=='D')
                    lf();
                else if (c=='Z')
                    respond(tty);
                else if (x=='7')
                    save_cur();
                else if (x=='8')
                    restore_cur();
                break;
            case 2:
                for(npar=0;npar<NPAR;npar++)
                    par[npar]=0;
                npar=0;
                state=3;
                if ((ques=(c=='?')))
                    break;
            case 3:
                if (c==';' && npar<NPAR-1) {
                    npar++;
                    break;
                } else if (c>='0' && c<='9') {
                    par[npar]=10*par[npar]+c-'0';
                    break;
                } else state=4;
            case 4:
                state=0;
                switch(c) {
                    case 'G': case '`':
                        if (par[0]) par[0]--;
                        gotoxy(par[0],y);
                        break;
                    case 'A':
                        if (!par[0]) par[0]++;
                        gotoxy(x,y-par[0]);
                        break;
                    case 'B': case 'e':
                        if (!par[0]) par[0]++;
                        gotoxy(x,y+par[0]);
                        break;
                    case 'C': case 'a':
                        if (!par[0]) par[0]++;
                        gotoxy(x+par[0],y);
                        break;
                    case 'D':
                        if (!par[0]) par[0]++;
                        gotoxy(x-par[0],y);
                        break;
                    case 'E':
                        if (!par[0]) par[0]++;
                        gotoxy(0,y+par[0]);
                        break;
                    case 'F':
                        if (!par[0]) par[0]++;
                        gotoxy(0,y-par[0]);
                        break;
                    case 'd':
                        if (par[0]) par[0]--;
                        gotoxy(x,par[0]);
                        break;
                    case 'H': case 'f':
                        if (par[0]) par[0]--;
                        if (par[1]) par[1]--;
                        gotoxy(par[1],par[0]);
                        break;
                    case 'J':
                        csi_J(par[0]);
                        break;
                    case 'K':
                        csi_K(par[0]);
                        break;
                    case 'L':
                        csi_L(par[0]);
                        break;
                    case 'M':
                        csi_M(par[0]);
                        break;
                    case 'P':
                        csi_P(par[0]);
                        break;
                    case '@':
                        csi_at(par[0]);
                        break;
                    case 'm':
                        csi_m();
                        break;
                    case 'r':
                        if (par[0]) par[0]--;
                        if (!par[1]) par[1] = video_num_lines;
                        if (par[0] < par[1] &&
                        par[1] <= video_num_lines) {
                            top=par[0];
                            bottom=par[1];
                        }
                        break;
                    case 's':
                        save_cur();
                        break;
                    case 'u':
                        restore_cur();
                        break;
                }
        }
    }
    set_cursor();
}

/*
 *  void con_init(void);
 *
 * This routine initalizes console interrupts, and does nothing
 * else. If you want the screen to clear, call tty_write with
 * the appropriate escape-sequece.
 *
 * Reads the information preserved by setup.s to determine the current display
 * type and sets everything accordingly.
 */
void con_init(void)
{
    register unsigned char a;
    char *display_desc = "????";
    char *display_ptr;

    video_num_columns = ORIG_VIDEO_COLS;
    video_size_row = video_num_columns * 2;
    video_num_lines = ORIG_VIDEO_LINES;
    video_page = ORIG_VIDEO_PAGE;
    video_erase_char = 0x0720;

    if (ORIG_VIDEO_MODE == 7)           /* Is this a monochrome display? */
    {
        video_mem_start = 0xb0000;
        video_port_reg = 0x3b4;
        video_port_val = 0x3b5;
        if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
        {
            video_type = VIDEO_TYPE_EGAM;
            video_mem_end = 0xb8000;
            display_desc = "EGAm";
        }
        else
        {
            video_type = VIDEO_TYPE_MDA;
            video_mem_end   = 0xb2000;
            display_desc = "*MDA";
        }
    }
    else                                /* If not, it is color. */
    {
        video_mem_start = 0xb8000;
        video_port_reg  = 0x3d4;
        video_port_val  = 0x3d5;
        if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
        {
            video_type = VIDEO_TYPE_EGAC;
            video_mem_end = 0xbc000;
            display_desc = "EGAc";
        }
        else
        {
            video_type = VIDEO_TYPE_CGA;
            video_mem_end = 0xba000;
            display_desc = "*CGA";
        }
    }

    /* Let the user known what kind of display driver we are using */

    display_ptr = ((char *)video_mem_start) + video_size_row - 8;
    while (*display_desc)
    {
        *display_ptr++ = *display_desc++;
        display_ptr++;
    }

    /* Initialize the variables used for scrolling (mostly EGA/VGA) */

    origin  = video_mem_start;
    scr_end = video_mem_start + video_num_lines * video_size_row;
    top = 0;
    bottom  = video_num_lines;

    gotoxy(ORIG_X,ORIG_Y);
    set_trap_gate(0x21,&keyboard_interrupt);
    outb_p(inb_p(0x21)&0xfd,0x21);
    a=inb_p(0x61);
    outb_p(a|0x80,0x61);
    outb(a,0x61);
}
/* from bsd-net-2: */

void sysbeepstop(void)
{
    /* disable counter 2 */
    outb(inb_p(0x61)&0xFC, 0x61);
}

int beepcount = 0;

static void sysbeep(void)
{
    /* enable counter 2 */
    outb_p(inb_p(0x61)|3, 0x61);
    /* set command for counter 2, 2 byte write */
    outb_p(0xB6, 0x43);
    /* send 0x637 for 750 HZ */
    outb_p(0x37, 0x42);
    outb(0x06, 0x42);
    /* 1/8 second */
    beepcount = HZ/8;   
}


read_write.c文件并不需要修改,具体原因见report(只是回显的话并不用修改该文件) 

1.在原始代码中,按下F12,中断响应后,中断服务程序会调用func?它实现的是什么功能? 
答:func函数的功能是把功能键扫描码变换成转义字符并存放到读队列中。判断是否为F1-F12的扫描码,若是,则将查func_table中的4个字节的转义字符序列放入缓冲队列中。 
若不是,则不处理并返回。例如:功能键发送的扫描码,F12键为:‘esc [ [ L’。

2.在你的实现中,是否把向文件输出的字符也过滤了?如果是,那么怎么能只过滤向终端输出的字符?如果不是,那么怎么能把向文件输出的字符也一并进行过滤? 
答:实现中并没有把向文件输出的字符也过滤,在copy_to_cooked函数处理中并不是将字符放到write_q,然后调用con_write来显示到控制台。 
而是shell通过上层的sys_write系统调用将tty->secondary队列中的字符显示出来的。而在sys_write的实现过程中调用了tty_write函数。 
所以只修改tty_write后,按键回显当然也变成‘’了。如果不使用shell那么回显的按键就是它本身,不会显示为‘’了。 
因此要想将向文件输出的字符一并进行过滤,需要修改file_write函数。设置Flag,如果为F12按下状态,将从内核态读出的数据转为‘*’赋给用户空间即可。 



哈工大操作系统实验5—I/O设备管理

标签:

原文地址:http://blog.csdn.net/wangjianyu0115/article/details/51247065

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!