2. ์์ฑํ ์ด์ ¶
OS๋ฅผ ๋ง๋ค์ด๋ณด๊ณ ์ถ์ ๋ง์์ ๋ฌด์์ ๋ฐ์ด ๋ค์๋๋ฐ ๋๋ถ๋ถ ํ๊ฒฝ์ ๋ฆฌ๋
์ค์์ด
gcc์์ต๋๋ค. ํ์ง๋ง ์ ๋ windows ํ๊ฒฝํ์ vc ๊ฐ๋ฐ์ ์ฃผ๋ก ํด์์ผ๋ฏ๋ก ๋ฌด์ฒ
๋ถํธ(?)ํ์ต๋๋ค. Djgpp ๋ผ๋ dos์ฉ gcc ํฌํ ๋ฒ์ ๊ณผ ์๋์ฐ์ฉ cygwin ํจํค์ง๋ฅผ
์ฌ์ฉํ๋ฉด ์ญ์ ๋์ผํ๊ฒ gcc๋ฅผ ์๋์ฐ์์ ์ปดํ์ผ ํ ์ ์์ต๋๋ค ๊ทธ๋๋ ์ ๋ vc์
์๋ํ ํ๊ฒฝ์ด ๋ง์ ๋ค์ด์ ๋๋ฒ๊น ๊น์ง๋ ํ๋ค๊ฒ ์ง๋ง (์ด์ฐจํผ ์ปค๋ ๋๋ฒ๊น ํ๋ ค๋ฉด
์ค์ค๋ก ์์ ์ ์ปค๋ ๋๋ฒ๊ฑฐ๋ฅผ ๋ง๋ค์ด์ผ ํ๋ ๊ฑฐ ๊ฐ์ต๋๋ค. ) ์๋ํ ๊ณผ ์ปดํ์ผ
๊ทธ๋ฆฌ๊ณ ๋ง์ปค๋ฅผ vc6๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์๊ณ ๊ทธ๋์ ์ด๋ฅผ ์ฌ์ฉํ ์ ์๋ ๋ฐฉ๋ฒ์ ์๊ฐํ๋ ค
ํฉ๋๋ค.
3. ์ฐธ๊ณ ํ๋ฉด ์ข์ ์ฌ์ดํธ๋ค ¶
์ด๋ ค์ด ์๋ฌธ OS๊ฐ๋ฐ๋ฐฉ๋ฒ์ ํ๊ธํ ํด์ฃผ๋ ๊ณ ๋ง์ด ์ฌ์ดํธ
Chobits os์ ์์ค๋ฅผ ์ป์ ์ ์์ต๋๋ค.
๋ฌด์ฒ ์ข์ Protected mode์ ๋ํ ์ค๋ช ๊ณผ ์์ค๋ฅผ ๋ด๊ณ ์๋ ์ฌ์ดํธ
http://www.nondot.org/sabre/os/articles
http://www.osdever.net/
http://mega-tokyo.com/osfaq2/
http://www.osdev.org/
http://www.bellona2.com
http://ksyspro.org
Chobits os์ ์์ค๋ฅผ ์ป์ ์ ์์ต๋๋ค.
๋ฌด์ฒ ์ข์ Protected mode์ ๋ํ ์ค๋ช ๊ณผ ์์ค๋ฅผ ๋ด๊ณ ์๋ ์ฌ์ดํธ
http://www.nondot.org/sabre/os/articles
-
OS์ ๋ํ ์ ๋ฐ์ ์ธ ์๋ฃ๊ฐ ์์ต๋๋ค.
http://www.osdever.net/
-
Bona Fide OS ๊ฐ๋ฐ ํํ ๋ฆฌ์ผ ์ฌ์ดํธ
http://mega-tokyo.com/osfaq2/
-
OS FAQ
http://www.osdev.org/
-
์์ค๊ฐ ๊ณต๊ฐ๋ ์ฌ๋ฌ os๊ฐ ์์ต๋๋ค.
http://www.bellona2.com
-
ํผ์์ GUI OS๋ฅผ ๋ง๋์๊ณ ์ฑ
๋ ์ถํํ์ ๋ถ์ ์ฌ์ดํธ
http://ksyspro.org
-
ํ๊ตญ ์๋ฒ ๋๋ ๊ฐ๋ฐ์ ๋ชจ์
4. Protected i386 OS๋ฅผ ์ํ 잛์ ์ค๋ช ¶
Intel์ ๋ค๋ฅธ cpu ๋ฒค๋๋ณด๋ค ์ญ์ฌ๊ฐ ์ค๋๋์ด์ 4bit microprocessor์ธ 4004์์
์ถ๋ฐํด์ 8bit cpu 8008, 8080, 16๋นํธ 8086, 80186, 80286, 32๋นํธ 80386, 80486,
80586 (๋๋ P5 ๊ทธ๋ฌ๋ ์ซ์๋ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ง ๋ชปํ๋ค๊ธธ๋ ์ดํ์ Pentium์ผ๋ก
๋ฐ๋์์ต๋๋ค ). ์ด๋ ๊ฒ ๊ธด ์ญ์ฌ๋ฅผ ๊ฐ๊ณ ์๋ ๋ณดํธ์ ์ธ cpu 8080 ๋๋ 8086 ํตํ์ด
intel x86 cpu์์ ๋๋ ํ๋ก๊ทธ๋จ๋ 586์์๋ ์ํ๋๋๋ก ํ์ํธํ์ ๊ฐ๊ฒ ๋ฉ๋๋ค.
์ด ํ์ํธํ ๋๋ฌธ์ ์๋ฌด๋ฆฌ ์ต์ ์ปดํจํฐ๋ ์ฒ์ ๋ถํ ์์๋ ๋น ๋ฅธ x86 ์ฒ๋ผ
i386+ ํ๊ฒฝ์ผ๋ก ๋ง๋ค๊ธฐ ์ํด์ ์ ์ด๋ ๋ค์๊ณผ ๊ฐ์ ์ผ์ ํ๊ฒ ๋ฉ๋๋ค.
ORG 0x7C00 - PC๊ฐ ๋ฆฌ์ ๋๋ฉด ๋ถํ ๋ ๋๋ฐ์ด์ค์ ์ฒซ๋ฒ์งธ ์นํฐ(512๋ฐ์ดํธ)๋ฅผ
์ฝ๋๋ ๊ทธ ๋ฉ๋ชจ๋ฆฌ์์ ์์ํ๋ค๊ณ ์ง์ ํด์ผ ํ ๊ฒ์ ๋๋ค.
A20 enable - ์์ ์๋ ์ค์ ๋ฉ๋ชจ๋ฆฌํฌ๊ธฐ๊ฐ ํฌ์ง ์์ 1M ์ดํ๋ง ์ ๊ทผํ๋๋ก
ํด์ฃผ์ด์ผ ํฉ๋๋ค.
Protected mode - ์ฒ์ ๋ถํ ํ ๋ x86 Real Mode๋ผ๋ ์ธ๊ทธ๋จผํธ:์คํ์
80386์์๋ 4G๊น์ง ์ ๊ทผ ๊ฐ๋ฅํ๋๋ก ๋์์ต๋๋ค.
์ปค๋๋ก๋ - ๋ถํธ์นํฐ ์ฝ๋๋ C๋ก๋ ๋ฐ์ด๋๋ฆฌ ์ปค๋์ ์ฌ๋ฌ ์นํฐ์์
๋ฉ๋๋ค.
5. ์ค๋น๋ฌผ ¶
- ์ปค๋์ ์ปดํ์ผ ๋ฐ ๋งํน์ ๋ด๋นํฉ๋๋ค.
- ์ฐ๋ฆฌ๋ ๋์ค์ผ๋ ์ฌ์ฉํ์ง ์๊ณ ๋ฐ๋ก ์ปดํ์ผํด์ ์ด๋ฏธ์ง๋ก ๋ฌ ํ VMWare๋ก
๋ก๋ํ ๊ฒ์ ๋๋ค. ( no ํ๋กํผ ๋ถํ ๋์ค์ผ, no ๋ฆฌ๋ถ, no test machine )
Nasm
- Netwide Asm์ผ๋ก at&t ๊ณ์ด ๋ฐ intel ๊ณ์ด ๋๋ค ์ง์ํ๊ณ target format๋
๋ง์ด ์ง์ํ๋ ์ด์ ๋ธ๋ฌ์ ๋๋ค. ์ค์น๊ณผ์ ์์ด ๊ทธ๋ฅ nasm.exe ๋ง ์์ผ๋ฉด ๋ฉ๋๋ค.
- ์ฐ๋ฆฌ๋ ๋์ค์ผ๋ ์ฌ์ฉํ์ง ์๊ณ ๋ฐ๋ก ์ปดํ์ผํด์ ์ด๋ฏธ์ง๋ก ๋ฌ ํ VMWare๋ก
๋ก๋ํ ๊ฒ์ ๋๋ค. ( no ํ๋กํผ ๋ถํ ๋์ค์ผ, no ๋ฆฌ๋ถ, no test machine )
Nasm
๋ง์ด ์ง์ํ๋ ์ด์ ๋ธ๋ฌ์ ๋๋ค. ์ค์น๊ณผ์ ์์ด ๊ทธ๋ฅ nasm.exe ๋ง ์์ผ๋ฉด ๋ฉ๋๋ค.
6. ์์ค ํ์ผ๋ค ¶
bootsect.asm
- ๊ฐ๋จํ ๋ณดํธ๋ชจ๋(์ธ๊ทธ๋จผํ
์ด์
๋ฐฉ์) ์ง์
๋ฐ ์ปค๋ ๋ก๋ ๋ถํธ๋ก๋
main.c
Makeboot.exe
๋ถํธ ์ด๋ฏธ์ง ๋จ๋ ํ๋ก๊ทธ๋จ ๋ง๋ค๋ฉด๋๋ค.
7.1. bootsect.asm ¶
[BITS 16] ; We need 16-bit intructions for Real
mode
[ORG 0x7C00] ; The BIOS loads the boot sector into memory location
0x7C00
reset_drive:
mov ah, 0 ; RESET-command
int 13h ; Call interrupt 13h
or ah, ah ; Check for error code
jnz reset_drive ; Try again if ah != 0
;vc์์ /base 10000 ๋ก 헀๊ธฐ ๋๋ฌธ์ ์ฌ๊ธฐ์๋ถํฐ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ก๋
; ํด์ผ ํ๋ค. ES:BX ๋ก 1000:0000h ์ด๊ธฐ ๋๋ฌธ์ ์๋์ฒ๋ผ ํ๋ค.
mov ax, 1000h ; es:bx <-- 1000:0000h
mov es, ax
mov bx, 0h ; Destination address = 0000:1000
mov ah, 02h ; READ SECTOR-command
; ๋๋ฒ์งธ ์นํฐ๋ถํฐ 3์นธ์นํฐ๊ฐ ์ปค๋์ด๋ฏธ์ง ์นํฐ๋ค.
mov al, 3h ; Number of sectors to read = 1
mov ch, 0 ; Cylinder = 0
; ํ์ฌ ์ฒซ๋ฒ์งธ์นํฐ512 ์ด๋ฉฐ ๋๋ฒ์งธ ์นํฐ๋ถํฐ 3๊ฐ ์นํฐ๊ฐ ์ปค๋์ด๋ฏธ์ง๋ค
mov cl, 02h ; Sector = 2
mov dh, 0 ; Head = 0
int 13h ; Call interrupt 13h
or ah, ah ; Check for error code
jnz reset_drive ; Try again if ah != 0
A20Address: ; Set A20 Address line here
CLI
CALL enableA20
STI
JMP Continue
enableA20:
call enableA20o1
jnz short enableA20done
mov al,0d1h
out 64h,al
call enableA20o1
jnz short enableA20done
mov al,0dfh
out 60h,al
enableA20o1:
mov ecx,20000h
enableA20o1l:
jmp short $+2
in al,64h
test al,2
loopnz enableA20o1l
enableA20done:
ret
Continue:
cli ; Disable interrupts, we want to be alone
xor ax, ax
mov ds, ax ; Set DS-register to 0 - used by lgdt
lgdt [gdt_desc] ; Load the GDT descriptor
mov eax, cr0 ; Copy the contents of CR0 into EAX
or eax, 1 ; Set bit 0
mov cr0, eax ; Copy the contents of EAX into CR0
jmp 08h:clear_pipe ; Jump to code segment, offset clear_pipe
[BITS 32] ; We now need 32-bit instructions
clear_pipe:
mov ax, 10h ; Save data segment identifyer
mov ds, ax ; Move a valid data segment into the data segment register
mov ss, ax ; Move a valid data segment into the stack segment register
mov esp, 090000h ; Move the stack pointer to 090000h
; 10200h๋ก ์ ํ 200h๋ vc๋ก ๋งํนํ๋ฉด ๊ธฐ๋ณธ PEํค๋(200h)๋ฅผ ๋ถ์ฌ์ ๋ํ๋ค.
; 10200h ์ฆ 1000:0200h ๋ก ์ ํ
;jmp 1000:0200h
jmp 10200h
gdt: ; Address for the GDT
gdt_null: ; Null Segment
dd 0
dd 0
gdt_code: ; Code segment, read/execute, nonconforming
dw 0FFFFh
dw 0
db 0
db 10011010b
db 11001111b
db 0
gdt_data: ; Data segment, read/write, expand down
dw 0FFFFh
dw 0
db 0
db 10010010b
db 11001111b
db 0
gdt_end: ; Used to calculate the size of the GDT
gdt_desc: ; The GDT descriptor
dw gdt_end - gdt - 1 ; Limit (size)
dd gdt ; Address of the GDT
times 510-($-$$) db 0 ; Fill up the file with zeros
dw 0AA55h ; Boot sector identifyer
์๋์ฒ๋ผ ์ด์
๋ธ ํ์ฌ ์ค๋ธ์ ๋ถํธ๋ก๋ ๋ฐ์ด๋๋ฆฌ๋ฅผ ์ป์ด๋ด์ผ
ํฉ๋๋ค.
nasm -f bin bootsect.asm -o bootsect.bin
๋ค์์ ๋ถํธ๋ก๋์ ์ปค๋ ์ด๋ฏธ์ง๋ฅผ ํฉ์ณ์ ํ๋์ ๋ถํ
์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ๋ makeboot.c
์ ๋๋ค. ๋ฐ๋ก ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด์ ๊ฐ๋จํ ์ปดํ์ผํด์ makeboot.exe๋ฅผ ๋ง๋ค๋๋ก
ํฉ๋๋ค.
๋ค์์ ์ปค๋ ์์ค์
๋๋ค.
ํฉ๋๋ค.
์ ๋๋ค. ๋ฐ๋ก ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด์ ๊ฐ๋จํ ์ปดํ์ผํด์ makeboot.exe๋ฅผ ๋ง๋ค๋๋ก
ํฉ๋๋ค.
#include
int
main(int argnr, char *args[])
{
FILE
*output, *input;
int
i, bytes_read, sectors_read, bytes_from_file;
char
buffer[512];
if
(argnr < 4) {
printf("Invalid
number of parameters.\n\n");
printf("USAGE:
%s [output] [input 1] [input 2] ... [input n]\n", args[0]);
printf("Example:
%s a.img bootsect.bin kernel.bin");
exit(0);
}
output
= fopen(args[1], "r");
//
๋ฎ์ด ์ฐ๊ธฐ ํ ๊บผ๋๊ณ ๋ฌผ์ด๋ณด๋ ์ฝ๋ ๋ฐ ๊ทธ๋ฅ ๋ฎ์ด์ฐ์. --
if
(output != NULL) {
buffer[0]
= 0;
}
fclose(output);
output
= fopen(args[1], "wb");
for
(i = 2; i < argnr; i++) {
input
= fopen(args[i], "rb");
if
(input == NULL) {
printf("Missing
input file %s. Aborting operation...", args[i]);
fclose(output);
exit(1);
}
bytes_read
= 512;
bytes_from_file
= 0;
sectors_read
= 0;
while(bytes_read
== 512 && !feof(input)) {
bytes_read
= fread(buffer, 1, 512, input);
if
(bytes_read == 0)
break;
if
(bytes_read != 512)
memset(buffer+bytes_read,
0, 512-bytes_read);
sectors_read++;
fwrite(buffer,
1, 512, output);
bytes_from_file
+= bytes_read;
}
printf("%d
sectors, %d bytes read from file %s...\n", sectors_read, bytes_from_file,
args[i]);
fclose(input);
}
fclose(output);
return
0;
}
//
bro (bro@shinbiro.com)2004-02-17 - initial ( vc dev envronment setting )
//
starting function
void
start()
{
char*
hello = "Hello OS!";
unsigned
char* vidmem = (unsigned char*)0xB8000;
while(
*hello != '\0' )
{
*vidmem++
= *hello++;
*vidmem++
= 7; // ๋ฌธ์ ๊ธฐ๋ณธ ์์ฑ
}
//
์ปค๋์ด ๋๋๋ฉด ์๋๋ค. ๊ณ์ ๋์์ผํ๋ค.
for(;;);
}
7.2. Visual C++ ์ปดํ์ผ ํ๊ฒฝ ๊ตฌ์ฑํ๊ธฐ ¶
/nologo /G4 /Zp1 /ML /W3 /vmg /vd0 /GX /Od /Gf /FAc /Fa"Release/" /Fo"Release/" /Fd"Release/" /FD /cLinkํญ์ ์ต์ ์ ์๋์ฒ๋ผ ํฉ๋๋ค.
/nologo /base:"0x10000" /entry:"start" /subsystem:console /incremental:no /pdb:"Release/testos.pdb" /map:"Release/testos.map" /machine:I386 /nodefaultlib /out:"testos.bin" /DRIVER /align:512 /FIXED๋ํ ์ปดํ์ผ ์์ ์์ ๋ฐ๋ก ๋ถํธ์นํฐ๋ ์ปดํ์ผํ๊ณ ์ปดํ์ผ ๋๋๋ฉด ๋ฐ๋ก ๋ถํธ ์ด๋ฏธ์ง๋
๋ง๋ค๋๋ก Pre-Link์ Post-Build๋ฅผ ์์ฑํฉ๋๋ค.
Deleting intermediate files and output files for project 'testos - Win32 Release'. --------------------Configuration: testos - Win32 Release-------------------- Compiling... main.c Linking... LINK : warning LNK4096: /BASE value "10000" is invalid for Windows 95; image may not run 1 sectors, 512 bytes read from file bootsect.bin... 3 sectors, 1536 bytes read from file testos.bin... testos.bin - 0 error(s), 1 warning(s)
8. VMWare ํ ์คํธ ํ๊ฒฝ ๊ตฌ์ฑํ๊ธฐ ¶
์ปดํ์ผ์ ๋ง์น๊ณ ๋๋ฉด ์๋์ ๊ฐ์ ํ์๊ธฐ ๋ชจ์ต์ด ๋ ๊ฒ์
๋๋ค. ๋ง์ง๋ง์ผ๋ก ์
์ฑ๋ testos.img ๊ฐ ์ฐ๋ฆฌ์ ์ปค๋ ์ด๋ฏธ์ง๊ฐ ๋ฉ๋๋ค.
Partcopy.exe ํด์ ์ฌ์ฉํ์ฌ ๋ถํ ๋์ค์ผ์ ๋์ ์๋ ์์ง๋ง ๋ฒ๊ฑฐ๋กญ์ต๋๋ค. ๋ฐ๋ผ์ VMWare์์ ์ง์ ์ด๋ฅผ ๋์ค์ผ ์ด๋ฏธ์ง๋ก ๋ก๋ํ๋๋ก ํฉ๋๋ค.
๋ฌด์กฐ๊ฑด ๊ทธ๋ฅ ๊ธฐ๋ณธ์ผ๋ก Next ํฉ๋๋ค.
์ ์ผ ์ค์ํ ๊ฒ์ ์ด๋ ๊ฒ ๋ง๋ ๊ฐ์ testos ์ ํ๋กํผ ๋์ค์ผ ๋ถํ ์ค์ ์ ๋๋ค.
Use Floppy Image์ ํด๋น img ํจ์ค ๋ง์ถ๋๋ก ํฉ๋๋ค.
์ด์ ์คํํด๋ด
๋๋ค.
Partcopy.exe ํด์ ์ฌ์ฉํ์ฌ ๋ถํ ๋์ค์ผ์ ๋์ ์๋ ์์ง๋ง ๋ฒ๊ฑฐ๋กญ์ต๋๋ค. ๋ฐ๋ผ์ VMWare์์ ์ง์ ์ด๋ฅผ ๋์ค์ผ ์ด๋ฏธ์ง๋ก ๋ก๋ํ๋๋ก ํฉ๋๋ค.
๋ฌด์กฐ๊ฑด ๊ทธ๋ฅ ๊ธฐ๋ณธ์ผ๋ก Next ํฉ๋๋ค.
์ ์ผ ์ค์ํ ๊ฒ์ ์ด๋ ๊ฒ ๋ง๋ ๊ฐ์ testos ์ ํ๋กํผ ๋์ค์ผ ๋ถํ ์ค์ ์ ๋๋ค.
Use Floppy Image์ ํด๋น img ํจ์ค ๋ง์ถ๋๋ก ํฉ๋๋ค.















