A Lecture on Computer Architecture For Readers

42
A Lecture on Computer Architecture For Readers

description

A Lecture on Computer Architecture For Readers. 프로그래머입장에서 바라본 컴퓨터 구조와 보호 모드 컴퓨터 구조에 대한 이해 ; 자신이 작성한 프로그램이 어떻게 수행될지 좀더 세밀하게 이해하기 위해서는 컴퓨터 구조에 대한 이해가 필수적이다. 정 덕 영 Software Design & Developer [email protected] [email protected]. Agenda. First Computer ENIAC von Neumann 80xx - PowerPoint PPT Presentation

Transcript of A Lecture on Computer Architecture For Readers

Page 1: A Lecture on Computer Architecture For Readers

A Lecture on

Computer ArchitectureFor Readers

Page 2: A Lecture on Computer Architecture For Readers

2

프로그래머입장에서 바라본

컴퓨터 구조와 보호 모드

컴퓨터 구조에 대한 이해;

자신이 작성한 프로그램이 어떻게 수행될지좀더 세밀하게 이해하기 위해서는 컴퓨터구조에 대한 이해가 필수적이다.

정 덕 영Software Design & Developer

[email protected] [email protected]

Page 3: A Lecture on Computer Architecture For Readers

3

Agenda

First Computer ENIAC von Neumann

80xx

Protected Mode Descriptor

PDE, PTE

IDT (Interrupt Descriptor Table)

I/O Privilege

Organization Pipe Line

Super scalar

Cache

Hyper Threading

Procedure & Stack Stack

Stack Frame

Calling Conventions

Stack Back Tracing

Page 4: A Lecture on Computer Architecture For Readers

4

First computer

Page 5: A Lecture on Computer Architecture For Readers

5

ENIAC - background

Electronic Numerical Integrator And Computer

University of Pennsylvania

Started 1943

Finished 1946 Too late for war effort

Used until 1955

Programmed manually by switches (hard wired)

18,000 vacuum tubes

30 tons

15,000 square feet

140 kW power consumption

5,000 additions per second

Page 6: A Lecture on Computer Architecture For Readers

6

von Neumann

Stored Program concept

Main memory storing programs and data

ALU operating on binary data

Control unit interpreting instructions from memory and executing

Input and output equipment operated by control unit

Princeton Institute for Advanced Studies IAS

Completed 1952

Page 7: A Lecture on Computer Architecture For Readers

7

Structure of von Neumann machine

Page 8: A Lecture on Computer Architecture For Readers

8

Structure of ISA (Instruction Set Architecture)

1000 x 40 bit words Binary number

2 x 20 bit instructions

Set of registers (storage in CPU) Memory Buffer Register

Memory Address Register

Instruction Register

Instruction Buffer Register

Program Counter

Accumulator

Multiplier Quotient

명령어가져오기

오퍼랜드가져오기

오퍼랜드 결과 저장

명령어 주소 계산

명령어해석

Source오퍼랜드

주소 계산

데이터연산

Destination오퍼랜드

주소 계산

프로세서와 외부 모듈과 의 데이터 교환

프로세서 내부의 동작

주소 지정 방식(Addressing Mode)

Page 9: A Lecture on Computer Architecture For Readers

9

80xx

Page 10: A Lecture on Computer Architecture For Readers

10

8086 Registers

AH AL

BH BL

CH CL

DH DL

SP

BP

SI

DI

IP

Flags

CS

DS

SS

ES

AX: Accumulator register

BX: Base register

CX: Count register

DX: Data register

Stack Pointer

Base Pointer

Source Index

Destination Index

Instruction Pointer

Status flags

Code Segment

Data Segment

Stack Segment

Extra Segment

0715

Page 11: A Lecture on Computer Architecture For Readers

11

8bit, 16bit

1101110000000010

0815 7

1500

8비트8비트

1100010000001001

0815 7

2500

8비트8비트

CPU

주소선택기

20Bit 주소 버스

16Bit 데이터 버스

메모리

/읽고쓰기

0 ~ 1,048,575

8 비트 크기

메모리

Page 12: A Lecture on Computer Architecture For Readers

12

80386 Registers

AH AL

BH BL

CH CL

DH DL

SP

BP

SI

DI

IP

Flags

CS

DS

SS

ES

AX: Accumulator register

BX: Base register

CX: Count register

DX: Data register

Stack Pointer

Base Pointer

Source Index

Destination Index

Instruction Pointer

Status flags

Segment Register

07151631

EAX

EBX

ECX

EDX

ESP

EBP

ESI

EDI

EIP

EFlags

CS

DS

SS

ES

SS

ES

FS

GS

General Register

031

Page fault linear address

CR0

CR1

CR2

Control Register

CR3

PG PEMPEMTSET

PDBR

Linear address for break point 0

Linear address for break point 1

Linear address for break point 2

Linear address for break point 3

DR0

DR1

DR2

DR3

DR4

DR5

Debug Register

B0B1B2B3BDBSBT

RW0len0RW1len1RW2len2RW3len3 GD L0G0L1G1L2G2L3G3LEG3

DR6

DR7

031

Linear Address W#WU#UD#DV CTR6

Physical Address RPHTR7

Test Register

031 12 11 10 9 8 7 6 5

5 4 2

Page 13: A Lecture on Computer Architecture For Readers

13

Protected Mode

Page 14: A Lecture on Computer Architecture For Readers

14

Protected Mode

OS CodePL = 0

PL = 1

PL = 2

Application CodePL = 3 특별한 방법

에 의해 접근

Applications

Operating SystemServices

Operating SystemKernel

Page 15: A Lecture on Computer Architecture For Readers

15

Protected Mode – Memory Access

세그먼트 레지스터 메모리 주소

SegmentDescriptor

Descriptor table

DIR Page Offset

Page

Page Directory

Page

Page Table 실제 메모리

Logical(Virtual) Address

Linear Address

PhysicalAddress

Segmentation

Paging

선형 주소

CR3

+

Page 16: A Lecture on Computer Architecture For Readers

16

GDT Size GDT Base Address

Entry n

...

...

Entry 1

Entry 0

Segment Descriptors…

Index TI

GDTR

RPL

Global DescriptorTable (GDT)

Entry n

...

...

Entry 1

Entry 0

Segment Descriptors…

Local DescriptorTable (LDT)

TI = 0 TI = 1

TI 0 GDT 비트가 일 경우 참조TI 1 LDT 비트가 일 경우 참조

Protected Mode – Descriptor Table

Index TI RPL

세그먼트 레지스터

15 23 0

0000000000011 0 111B

0000000000001 0 00 8

Page 17: A Lecture on Computer Architecture For Readers

17

Base Bits(31 ~ 24)

G D X ULimit Bits(19 ~16)

P DPL S TYPEBase Bits(23 ~ 0)

Limit Bits(15 ~ 0)

63 56 55 52 51 48 47 44 39 16 15 0

(System Segment descriptor) 시스템 세그먼트 디스크립터

0 시스템 비트가 로 세팅되어짐

Base Bits(31 ~ 24)

G D X ULimit Bits(19~16)

P DPL S TYPE ABase Bits(23 ~ 0)

Limit Bits(15 ~ 0)

63 56 55 52 51 48 47 44 40 39 16 15 0

System 1 비트가 로 세팅된 경우

1 으로 세팅된 경우 코드로 사용됨

Code/Data

Conforming Read

414243

Base Bits(31 ~ 24)

G D X ULimit Bits(19~16)

P DPL S TYPE ABase Bits(23 ~ 0)

Limit Bits(15 ~ 0)

63 56 55 52 51 48 47 44 40 39 16 15 0

Code/Data

Expand-Down

Write

414243

0 으로 세팅된 경우 데이터로 사용됨

Protected Mode – Descriptor

Page 18: A Lecture on Computer Architecture For Readers

18

Protected Mode – updating segment

데이터 세그먼트 갱신 (DS, ES, FS, GS)

스택 세그먼트 갱신 (SS)

Page 19: A Lecture on Computer Architecture For Readers

19

Protected Mode – call gate

코드 세그먼트 갱신 (CS)

Call gateDescriptor

Code Descriptor

셀렉터 오프셋:

디스크립터 테이블 행선 코드 세그먼트

콜게이트의오프셋

셀렉터

PARAM3

RING 3 스택

PARAM2

PARAM1

SS : ESP

031 낮은 주소

높은 주소

PARAM3

RING 0 스택

PARAM2

PARAM1

SS : ESP

031 낮은 주소

높은 주소XX SS

ESP

XX CS

EIP

콜게이트

TSS 의SS0:ESP0

Page 20: A Lecture on Computer Architecture For Readers

20

32Bit Linear Base Address 16Bit Segment Limit

47 16 15 0

IDTR Register

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

0 Entry

255 Entry

IDT(Interrupt Descriptor Table)

Protected Mode – Jump Kernel using Interupt

17 16

NT

15

IOPL

12

O

11

D

10

I

9

T

8

S

7

Z

6 5

A

4 3

P

2 1

C

0

RFVM

14

AC

18

User Application

Kernel32.dll Win32 API에 정의되어 있는

ntdll.dll native API에 정의되어 있는

Int 2E

System Call Layer

NT executive

User Level

Kernel Level

CreateFile() 호출

NTCreateFile() 호출

Ntoskrnl.exe의KiSystemService() 호출

Ntoskrnl.exe의NTCreateFile() 호출

(user32.dll, gdi32.dll, imm32.dll, winsrv.dll)

Page 21: A Lecture on Computer Architecture For Readers

21

Protected Mode – Paging

매핑된 페이지 프레임 번호

페이지 테이블

0

1

0

실제 메모리

4096

8192

0페이지 프레임

1페이지 프레임

2페이지 프레임

3페이지 프레임

4페이지 프레임

5페이지 프레임

6페이지 프레임

7페이지 프레임

매핑된 페이지 프레임 번호

가상 메모리 번지

012

000000000 (offset)변위

A프로세스

0

4096

B프로세스

0

4096

C프로세스

0

4096

0

실제 메모리

4096

8192

0페이지 프레임

1페이지 프레임

2페이지 프레임

3페이지 프레임

4페이지 프레임

5페이지 프레임

6페이지 프레임

7페이지 프레임

Selector Offset

SegmentDescriptor

Descriptor table

+

DIR Page Offset

Page

Page DirectoryPage Table

CR3 실제 메모리

Logical(Virtual) Address( )가상 메모리

Linear Address( )선형 메모리

PhysicalAddress

( )물리적 메모리

Segmentation

Paging

(CS, DS, ES, SS, FS, GS)세그먼트 레지스터

선형 주소

Page Page

사용자가 지정한 주소

최종적으로 얻어진 물리 주소

매핑된 페이지 프레임 번호

페이지 테이블

000002

0

1

0

실제 메모리

4096

8192

0페이지 프레임

1페이지 프레임

2페이지 프레임

3페이지 프레임

4페이지 프레임

5페이지 프레임

6페이지 프레임

7페이지 프레임

매핑된 페이지 프레임 번호

가상 메모리 번지

012

000000001 0000000000010x1001

8193

Page 22: A Lecture on Computer Architecture For Readers

22

10 bits(page directory index)

31

10 bits(page table index)

12 bits(offset in page)

21 11 0

CR3register

Page Directory Page Tables

실제의 물리 메모리

32 (Linear Address)비트의 선형 주소

4KB Pages

4KB Pages

4KB Pages

10 bits(page directory index)

31

10 bits(page table index)

12 bits(offset in page)

21 11 0

CR3register

Page Directory Page Tables

실제의 물리 메모리

32 (Linear Address)비트의 선형 주소

4KB Pages

4KB Pages

4KB Pages

Protected Mode – Paging

Page 23: A Lecture on Computer Architecture For Readers

23

Protected Mode – PDE, PTE

000000000000 PFN(Page Frame Number)페이지 디렉토리의1101100010000011 0xD883

34

CR3 (Control Register 3)

1131

PFN (31~12)페이지 테이블의 실번지 AVIL 0PS

D APCD

PWT

US

RW

P

0133456789111231

, 페이지 디렉토리 테이블 엔트리

10 bits(page directory index)

31

10 bits(page table index)

12 bits(offset in page)

21 11 0

CR3register

Page Directory Page Tables

실제의 물리 메모리

32 (Linear Address)비트의 선형 주소

4KB Pages

4KB Pages

4KB Pages

커널 영역

유저 영역

Page 24: A Lecture on Computer Architecture For Readers

24

Protected Mode – I/O Privilege

Back Link0

01531

0

ESP 0 4

SS 00 8

ESP 1 C

SS 10 10

ESP 2 14

SS 20 18

CR3 1C

EIP 20

EFLAGS 24

EAX 28

ECX 2C

EDX 30

EBX 34

ESP 38

EBP 3C

ESI 40

EDI 44

ES0 48

CS0 4C

SS0 50

DS0 54

FS0 58

GS0 5C

LDT0 60

0I/O 번지 비트마스크 오프셋 64T

7 015 823 1631 24

39 3247 4055 4863 56

65535

GDT Size GDT Base Address

Entry n

Task gate descriptor

Task gate descriptor

Entry 0

Segment Descriptors…

Index

GDTR

Global DescriptorTable (GDT)

TSS(Task State Segment)

TR(Task Register)

315

17 16

NT

15

IOPL

12

O

11

D

10

I

9

T

8

S

7

Z

6 5

A

4 3

P

2 1

C

0

RFVM

14

AC

18

Page 25: A Lecture on Computer Architecture For Readers

25

Protected Mode – IDT (Interrupt Descriptor Table)

17 16

NT

15

IOPL

12

O

11

D

10

I

9

T

8

S

7

Z

6 5

A

4 3

P

2 1

C

0

RFVM

14

AC

18

32Bit Linear Base Address 16Bit Segment Limit

47 16 15 0

IDTR Register

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

Attributes Code Selector 32Bit Offset

0 Entry

255 Entry

IDT(Interrupt Descriptor Table)

Page 26: A Lecture on Computer Architecture For Readers

26

Exception, Interrupt (Intel)

PFN (31~12)페이지 테이블의 실번지 AVIL 0PS

D APCD

PWT

US

RW

P

0133456789111231

, 페이지 디렉토리 테이블 엔트리

Page 27: A Lecture on Computer Architecture For Readers

27

Page Fault Handler

MOV EAX [0x40000]

0

P

Page Table

Virtual Memory

Disk 장치

0x10000

0x40000

Physical Memory

운영체제의Page Fault Handler

(1)

Page Table 검색

(4)Disk로부터

데이터를 읽어옴

(5)Page Table 을갱신함

(6) 명령어를 재시작함

(2)Exception 발생

(3)Disk 로

읽기 요청

Page 28: A Lecture on Computer Architecture For Readers

28

Organization

Page 29: A Lecture on Computer Architecture For Readers

29

80486 – Pipe Line

명령어 패치 명령어 해석 및 유효 번지 계산

오퍼랜드패치 실행

1 단계 2 단계 3 단계 4 단계

기록

5 단계

1단계 2단계 3단계 4단계 2단계1단계 3단계 4단계

시간( )클록 수

1 명령어 끝남 2 명령어 끝남

시간( )클록 수

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1명령어

2명령어

3명령어

4명령어

5단계 5단계

5단계

5단계

5단계

5단계

1단계 2단계 3단계 4단계 5단계

1단계 2단계 3단계 4단계 5단계

5명령어

6명령어

명령어가져오기

오퍼랜드가져오기

오퍼랜드 결과 저장

명령어 주소 계산

명령어해석

Source오퍼랜드

주소 계산

데이터연산

Destination오퍼랜드

주소 계산

프로세서와 외부 모듈과 의 데이터 교환

프로세서 내부의 동작

주소 지정 방식(Addressing Mode)

Page 30: A Lecture on Computer Architecture For Readers

30

Pentium – Super scalar

시간( )클록 수

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1명령어

2명령어

3명령어

4명령어

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

5명령어

6명령어

7명령어

8명령어

5단계

5단계

5단계

5단계

5단계

5단계

5단계

5단계

1단계 2단계 3단계 4단계

1단계 2단계 3단계 4단계

5단계

5단계 9명령어

10명령어

Bus Unit

Code cache

Prefetch buffer

ALUU- pipeline

ALUV- pipeline

Registers

Data cache

Branch PredictionBuffer

Floating point Pipeline

Floating Point Arithmetic

64 Bit

64 Bit

64 Bit256 Bit

32 Bit 32 Bit

32 Bit 32 Bit

32 Bit 64 Bit

Page 31: A Lecture on Computer Architecture For Readers

31

00

01

FF

01

FF

12345678

데이터Line 번호

0000

0001

0002

0003

00

Tag 값

11111111

22222222

01

37593818FF

12345678

11111111

37593818

22222222

Tag 값

77777777

데이터 Line 번호

0000

0001

0002

0003

777777773FFF

0000

0001

0002

0003

0000

0001

0002

0003

3FFF

16- MByte 의 메인 메모리

64- KByte Cache의

8 14 2

Tag Line Word

메인 메모리의 주소

실제 주소

000000

000004

010000

010004

FF0000

FF0004

FFFFF0

FFFFFC

Cache - Direct mapping

마이크로프로세서 캐쉬 메인 메모리

워드 사이즈 사이즈

00

01

FF

01

FF

12345678

데이터Line 번호

0000

0001

0002

0003

00

Tag 값

11111111

22222222

01

37593818FF

12345678

11111111

37593818

22222222

Tag 값

77777777

데이터 Line 번호

0000

0001

0002

0003

777777773FFF

0000

0001

0002

0003

0000

0001

0002

0003

3FFF

16- MByte 의 메인 메모리

64- KByte Cache의

8 14 2

Tag Line Word

메인 메모리의 주소

실제 주소

000000

000004

010000

010004

FF0000

FF0004

FFFFF0

FFFFFC

Page 32: A Lecture on Computer Architecture For Readers

32

Cache - Associative mapping

000000

004001

3FC001

004003

3FFFFF

12345678

데이터

11111111

22222222

37593818

12345678

11111111

37593818

22222222

Tag 값

77777777

데이터

77777777

16- MByte 의 메인 메모리

64- KByte Cache의

22 2

Tag Word

메인 메모리의 주소

실제 주소

000000

000004

010004

FF0000

FF0004

FFFFF0

FFFFFC

01000C

22 Bits 32 Bits

32 Bits

000000

004001

3FC001

004003

3FFFFF

12345678

11111111

37593818

22222222

Tag 값

77777777

데이터

64- KByte Cache의

22 Bits 32 Bits

Word

Page 33: A Lecture on Computer Architecture For Readers

33

12345678

데이터Line 번호

0000

0001

0002

0003

000

Tag 값

10248282

11111111

22222222

001

37593818

1FF

12345678

11111111

37593818

000

Tag 값

001

1FF

데이터 Line 번호

0000

0001

0002

0003

777777771FFF

0000

0001

0002

0003

0000

0001

0002

0003

1FFF

16- MByte 의 메인 메모리

2- way Set 64- KByte Cache의

9 13 2

Tag Line Word

메인 메모리의 주소

실제 주소

000000

000004

008000

008004

FF8000

FF8004

FFFFF0

FFFFFC

FF8008

00800C

008008

10248282

22222222

001

Tag 값

001

777777771FF

데이터

9 Bits 32 Bits 9 Bits 32 Bits

32 Bits

32KByte 1 Set의 번 32KByte 2 Set의 번

Cache - Set Associative mapping

Page 34: A Lecture on Computer Architecture For Readers

34

Pentium’s cache

Page 35: A Lecture on Computer Architecture For Readers

35

Pentium 4(xeon) – Hyper Threading

Processor 2

Processor 1

Page 36: A Lecture on Computer Architecture For Readers

36

Procedure & Stack

Page 37: A Lecture on Computer Architecture For Readers

37

Stack

Main() Return Address

Local1 변수의 저장 위치 ESP

1. Main Stack 함수 시작 시의 모양

Main() Return Address

Local1 변수의 저장 위치

ESP

2. A Stack 함수 시작 시의 모양

A() Return Address

Local2 변수의 저장 위치

Local3 변수의 저장 위치

Main() Return Address

Local1 변수의 저장 위치

ESP

A() Return Address

Local2 변수의 저장 위치

Local3 변수의 저장 위치

3. B Stack 함수 시작 시의 모양

B() Return Address

Local4 변수의 저장 위치

Main() Return Address

Local1 변수의 저장 위치

ESP

A() Return Address

Local2 변수의 저장 위치

Local3 변수의 저장 위치

4. D Stack 함수 시작 시의 모양

B() Return Address

Local4 변수의 저장 위치

D() Return Address

Local6 변수의 저장 위치

Main() Return Address

Local1 변수의 저장 위치

ESP

A() Return Address

Local2 변수의 저장 위치

Local3 변수의 저장 위치

5. D Return함수 B Return함수 C 후에 함수 호출 바로 직전의 A 함수에서의 모양

B() Return Address

Local4 변수의 저장 위치

D() Return Address

Local6 변수의 저장 위치

Main() Return Address

Local1 변수의 저장 위치

ESP

A() Return Address

Local2 변수의 저장 위치

Local3 변수의 저장 위치

6. C Stack 함수 시작 시의 모양

C() Return Address

Local5 변수의 저장 위치

D() Return Address

Local6 변수의 저장 위치

Main() Return Address

Local1 변수의 저장 위치 ESPA() Return Address

Local2 변수의 저장 위치

Local3 변수의 저장 위치

7. Return모든 함수가 다 되어지고 main 함수로 돌아왔을 때의 스택

C() Return Address

Local5 변수의 저장 위치

D() Return Address

Local6 변수의 저장 위치

Page 38: A Lecture on Computer Architecture For Readers

38

Stack Frame

Main() 복귀 주소

2

1

Sum() 복귀 주소

ESP + 0x4

ESP + 0x8

ESP + 0xc

ESP

Page 39: A Lecture on Computer Architecture For Readers

39

Calling Conventions

__cdecl __stdcall

Page 40: A Lecture on Computer Architecture For Readers

40

Stack Back Tracing

Main() 복귀 주소

EBP이전

Local p( :”Test”)변수 값

0 파라미터 값

Local a변수

A() 복귀 주소

Local b변수

p ( :0)파라미터 값 값

B() 복귀 주소

EBP이전

EBP이전

Local c변수

EBP

ESP

Page 41: A Lecture on Computer Architecture For Readers

41

Question And Answer

Page 42: A Lecture on Computer Architecture For Readers

42

GOOD BYEGOOD BYE