Top Banner
Using x86 “protected mode” on our anchor- cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism
31

Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Using x86 “protected mode” on our anchor-cluster’s machines

A look at some terminal emulation features utilized in the “console-

redirection” mechanism

Page 2: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Our remote-access scheme

rackmountPC system

‘pyramid’

studentworkstation

KVM cable

ethernet cables

‘anchor08’

‘anchor07’

‘anchor06’

‘anchor05’

‘anchor04’

‘anchor03’

‘anchor02’

‘anchor01’

sixteenCore 2 Duo systems

CS file-server

null-modemserial cables

. . .

‘telnet’ application

Page 3: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

A programming hurdle

• When you try to test a ‘boot-time’ program on one of our ‘anchor’ machines, you will encounter a problem when your program enters protected-mode: you can no longer see the current screen-output, nor control your program using keyboard-input, since the normal ‘console redirection’ capability (which ‘telnet’ relies on) has been disabled

Page 4: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

The console’s output

• At boot-time the ‘anchor’ machines use a ‘real-mode’ interrupt-handler in the BIOS to continually transfer information (via the null-modem serial-cable) from the display memory of the local machine to the ‘telnet’ program running on the ‘pyramid’ server

• From there it gets sent over the Ethernet network to a student’s computer screen

Page 5: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

The console’s input

• When a student types a keystroke, it gets sent via the network to the ‘telnet’ program running on ‘pyramid’, whereupon it then is forwarded to the ‘anchor’ machine via the null-modem serial-cable connection

• This scheme allows us to do our work on the anchor-machines remotely – from our classroom or CS Labs, or even from home

Page 6: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

The IVT’s role

• But this scheme relies on BIOS code that handle’s UART interrupts in ‘real-mode’

• When a program enters ‘protected-mode’, it has to switch from its Table of Interrupt Vectors (used in real-mode) to a different table for dispatching of interrupts to ISRs

• At that point we lose all the functionality that BIOS’s interrupt-handlers provided!

Page 7: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Our reimplementation

• Just as we’ve seen how to regain some of the functionality provided by the real-mode BIOS code, by writing our own protected-mode interrupt-handlers for the keyboard and the timer, we can restore the basic ‘console redirection’ capability by writing our own interrupt-handler for the UART when the CPU is in its ‘protected-mode’

Page 8: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Some details

• We need to write “trivial” interrupt-handlers for a few peripherals besides the UART:– For the timer (so we can see something that’s

changing on the display screen)– For the keyboard (so we can control when to

quit watching our display)– For any ‘spurious’ interrupts that the 8259 PIC

delivers to the CPU (so we won’t “crash” due to unwanted General Protection Exceptions)

Page 9: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Idea for UART’s RX-interrupts

• When our UART receives a new scancode sent by the ‘telnet’ program on ‘pyramid’ we need it to be treated as if it were from the anchor-machine’s keyboard – but the anchor’s keyboard is out-of-our-reach

• So we issue a command to the keyboard-controller to write that scancode into its Output Buffer register (command 0xD2)

Page 10: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Idea for UART’s TX-interrupts

• When the UART issues its THRE-interrupt (Transmitter Holding Register Empty), we want to output a new byte of information from the anchor-machine’s video memory

• The video memory consists of alternating ascii-codes and attribute-codes, requiring distinct treatments for proper display on the remote terminal (or Desktop window)

Page 11: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Clearing the screen

• Here is an ANSI command-sequence that clears the terminal’s display-screen:

char cmd[] = “\033[2J”;

int len = strlen( cmd );

write( 1, cmd, len );

Page 12: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Reposition the cursor

• Here is an ANSI command-sequence that moves the cursor to row 12, column 40:

char cmd[] = “\033[12;40H”;

int len = strlen( cmd );

write( 1, cmd, len );

Page 13: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Format of ‘attribute’ bytes

BLINKBGred

BGgreen

BGblue

FGintense

FGred

FGgreen

FGblue

VGA text-color attributes byte

7 6 5 4 3 2 1 0

foreground color background color

Page 14: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

ANSI color-codes

0 = black1 = red2 = green3 = brown4 = blue5 = magenta6 = cyan7 = gray

Page 15: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Color-translation table

IBM VGA DEC ANSI--------------------------------------------------------------------------------000 = black 000 = black001 = blue 100 = blue010 = green 010 = green011 = cyan 110 = cyan100 = red 001 = red101 = magenta 101 = magenta110 = brown 011 = brown111 = gray 111 = gray--------------------------------------------------------------------------------

Page 16: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Setting text attributes

• Here is an ANSI command-sequence that sets foreground and background colors:

char cmd[] = “\033[32;44m”;

int len = strlen( cmd );

write( 1, cmd, len );

Page 17: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Cursor visibility commands

• Here are ANSI command-sequences that will ‘hide’ or ‘show’ the terminal’s cursor:

char hide[] = “\033[?25l”; // lowercase L

char show[] = “\033[?25h”; // lowercase H

Page 18: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

‘console output’ algorithmunsigned char color_table[ 8 ] = { 0, 4, 2, 6, 1, 5, 3, 7 };

typedef struct { unsigned char ascii, color; } PEL;PEL *vram = (PEL *)0x000B8000;PEL prev = { 0, 0 };

for (int row = 0; row < 24; row++){printf( “\033[?25l” ); // make the cursor invisibleprintf( “\033[%d;%dH”, row+1, 1 ); // cursor to row’s beginningfor (int col = 0; col < 80; col++)

{PEL curr = vram[ row*80 + col ];if ( curr.color != prev.color )

{int fg = color_table[ (curr.color >> 0)&7 ];int bg = color_table[ (curr.color >> 4)&7 ];printf( “\033[%d;%dm”, fg + 30, bg + 40 );}

printf( “%c”, curr.ascii );prev = curr;}

printf( “\033[?25h” ); // make the cursor visiblefflush( stdout ); // insure all data written }

Page 19: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Programming interface

RxD/TxD IER IIR/FCR LCR MCR LSR MSR SCR

The PC uses eight consecutive I/O-ports to access the UART’s registers

0x03F8 0x03F9 0x03FA 0x03FB 0x03FC 0x03FD 0x03FE 0x03FF

scratchpad register

modem statusregister

line statusregister

modem controlregister

line controlregister

interrupt enableregister

interrupt identification register and FIFO control register

receive buffer register and transmitter holding register(also Divisor Latch register)

Page 20: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Modem Control Register

0 0 0LOOPBACK

OUT2 OUT1 RTS DTR

7 6 5 4 3 2 1 0

Legend: DTR = Data Terminal Ready (1=yes, 0=no) RTS = Request To Send (1=yes, 0=no) OUT1 = not used (except in loopback mode) OUT2 = enables the UART to issue interrupts LOOPBACK-mode (1=enabled, 0=disabled)

Page 21: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Modem Status Register

DCD RI DSR CTSdeltaDCD

deltaRI

deltaDSR

deltaCTS

7 6 5 4 3 2 1 0

set if the corresponding bit has changed since the last time this register was read

Legend: [---- loopback-mode ----] CTS = Clear To Send (1=yes, 0=no) [bit 0 in Modem Control] DSR = Data Set Ready (1=yes, 0=no) [bit 1 in Modem Control] RI = Ring Indicator (1=yes,0=no) [bit 2 in Modem Control] DCD = Data Carrier Detected (1=yes,0=no) [bit 3 in Modem Control]

Page 22: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Line Status Register

Error inRx FIFO

Transmitteridle

THRempty

Breakinterrupt

Framingerror

Parityerror

Overrunerror

ReceivedData

Ready

7 6 5 4 3 2 1 0

These status-bits indicate errors in the received data

This status-bit indicates that a new byte of data has arrived(or, in FIFO-mode, that the receiver-FIFO has reached its threshold)

This status-bitindicates that thedata-transmission has been completed

This status-bit indicates that the Transmitter Holding Register is ready to accept a new data byte

Page 23: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Line Control Register

DivisorLatch

access

setbreak

stickparity

even parityselect

parityenable

numberof stop

bits

word lengthselection

7 6 5 4 3 2 1 0

00 = 5 bits01 = 6 bits10 = 7 bits11 = 8 bits

0 = 1 stop bit1 = 2 stop bits

0 = no parity bits1 = one parity bit

1 = even parity0 = ‘odd’ parity

0 = not accessible1 = assessible

0 = normal1 = ‘break’

Page 24: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Interrupt Enable Register

0 0 0 0ModemStatuschange

Rx LineStatuschange

THRis

empty

Receiveddata is

available

7 6 5 4 3 2 1 0

If enabled (by setting the bit to 1),the UART will generate an interrupt:(bit 3) whenever modem status changes(bit 2) whenever a receive-error is detected (bit 1) whenever the transmit-buffer is empty(bit 0) whenever the receive-buffer is nonempty

Also, in FIFO mode, a ‘timeout’ interrupt will be generated if neither FIFO has been ‘serviced’ for at least four character-clock times

Page 25: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

FIFO Control Register

RCVR FIFOtrigger-level

reserved reservedDMAModeselect

XMITFIFOreset

RCVRFIFOreset

FIFOenable

7 6 5 4 3 2 1 0

Writing 0 will disable the UART’s FIFO-mode, writing 1 will enable FIFO-mode

Writing 1 empties the FIFO, writing 0 has no effect

00 = 1 byte01 = 4 bytes10 = 8 bytes11 = 14 bytes

NOTE: DMA is unsupported for the UART on our systems

Page 26: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Interrupt Identification Register

0 0

7 6 5 4 3 2 1 0

00 = FIFO-mode has not been enabled 11 = FIFO-mode is currently enabled

1 = No UART interrupts are pending0 = At least one UART interrupt is pending

‘highest priority’ UART interrupt still pendinghighest

011 = receiver line-status 010 = received data ready 110 = character timeout 001 = Tx Holding Reg empty 000 = modem-status changelowest

Page 27: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Response to UART interrupts

• In order of highest-to-lowest priority:– 0x06:Receive errors (input Line Status)– 0x04:Received data (input Rx-Data)– 0x0C: FIFO Timeout (input/output Data)– 0x02:THRE (output Tx-Data, or input IIR)– 0x00:Modem state (input Modem Status)

Page 28: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

How to transmit a byte

Read the Line Status Register

Write byte to the Transmitter Data Register

Transmit Holding Registeris Empty?NO

YES

DONE

Page 29: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

How to receive a byte

Read the Line Status Register

Read byte from the Receiver Data Register

Received Datais Ready?NO

YES

DONE

Page 30: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

Demo-program

• We wrote a boot-time program (it’s called ‘xmitvram.s’) that we can run on one of our anchor-cluster machines

• It shows how a protected-mode interrupt-handler for the serial UART interrupts can send ANSI terminal-control strings (along with ASCII character-codes) to the ‘telnet’ terminal emulator application on ‘pyramid’

Page 31: Using x86 “protected mode” on our anchor-cluster’s machines A look at some terminal emulation features utilized in the “console- redirection” mechanism.

In-class exercise

• Modify this simple C++ program so that it will print its “Hello” message in colors and be located in the center of the screen:

#include <stdio.h>

int main( void ){

printf( “Hello, world! \n” );}