Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cm20170909
os-tutorial
提交
de3d4425
O
os-tutorial
项目概览
cm20170909
/
os-tutorial
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
os-tutorial
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
de3d4425
编写于
8月 18, 2015
作者:
C
Carlos
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
lesson 23, step 3
上级
92ff191c
变更
18
隐藏空白更改
内联
并排
Showing
18 changed file
with
124 addition
and
127 deletion
+124
-127
23-fixes/README.md
23-fixes/README.md
+11
-7
23-fixes/cpu/idt.c
23-fixes/cpu/idt.c
+3
-2
23-fixes/cpu/idt.h
23-fixes/cpu/idt.h
+9
-9
23-fixes/cpu/isr.c
23-fixes/cpu/isr.c
+49
-49
23-fixes/cpu/isr.h
23-fixes/cpu/isr.h
+6
-6
23-fixes/cpu/ports.c
23-fixes/cpu/ports.c
+6
-6
23-fixes/cpu/ports.h
23-fixes/cpu/ports.h
+5
-5
23-fixes/cpu/timer.c
23-fixes/cpu/timer.c
+5
-5
23-fixes/cpu/timer.h
23-fixes/cpu/timer.h
+2
-2
23-fixes/cpu/type.h
23-fixes/cpu/type.h
+3
-10
23-fixes/drivers/keyboard.c
23-fixes/drivers/keyboard.c
+2
-1
23-fixes/drivers/keyboard.h
23-fixes/drivers/keyboard.h
+0
-2
23-fixes/drivers/screen.c
23-fixes/drivers/screen.c
+8
-7
23-fixes/drivers/screen.h
23-fixes/drivers/screen.h
+0
-2
23-fixes/kernel/kernel.c
23-fixes/kernel/kernel.c
+3
-2
23-fixes/libc/mem.c
23-fixes/libc/mem.c
+6
-6
23-fixes/libc/mem.h
23-fixes/libc/mem.h
+4
-4
23-fixes/libc/string.c
23-fixes/libc/string.c
+2
-2
未找到文件。
23-fixes/README.md
浏览文件 @
de3d4425
...
@@ -16,13 +16,9 @@ We add `-ffreestanding` when compiling `.o` files, which includes `kernel_entry
...
@@ -16,13 +16,9 @@ We add `-ffreestanding` when compiling `.o` files, which includes `kernel_entry
Before, we disabled libgcc (not libc) through the use of
`-nostdlib`
and we didn't re-enable
Before, we disabled libgcc (not libc) through the use of
`-nostdlib`
and we didn't re-enable
it for linking. Since this is tricky, we'll delete
`-nostdlib`
it for linking. Since this is tricky, we'll delete
`-nostdlib`
`-nostdinc`
was also pased to gcc, but we will need it for step 3.
2. Not setting a stack
2.
kernel.c
`main()`
function
----------------------
3. kernel.c `main()` function
-----------------------------
-----------------------------
Modify
`kernel/kernel.c`
and change
`main()`
to
`kernel_main()`
since gcc recognizes "main" as
Modify
`kernel/kernel.c`
and change
`main()`
to
`kernel_main()`
since gcc recognizes "main" as
...
@@ -33,8 +29,16 @@ Change `boot/kernel_entry.asm` to point to the new name accordingly.
...
@@ -33,8 +29,16 @@ Change `boot/kernel_entry.asm` to point to the new name accordingly.
To fix the
`i386-elf-ld: warning: cannot find entry symbol _start; defaulting to 0000000000001000`
To fix the
`i386-elf-ld: warning: cannot find entry symbol _start; defaulting to 0000000000001000`
warning message, add a
`global _start;`
and define the
`_start:`
label in
`boot/kernel_entry.asm`
.
warning message, add a
`global _start;`
and define the
`_start:`
label in
`boot/kernel_entry.asm`
.
4.
Reinvented datatypes
3. Reinvented datatypes
-----------------------
-----------------------
It looks like it was a bad idea to define non-standard data types like
`u32`
and such, since
C99 introduces standard fixed-width data types like
`uint32_t`
We need to include
`<stdint.h>`
which works even in
`-ffreestanding`
(but requires stdlibs)
and use those data types instead of our own, then delete them on
`type.h`
<stddef.h>
to provide size
\_
t
<stddef.h>
to provide size
\_
t
...
...
23-fixes/cpu/idt.c
浏览文件 @
de3d4425
#include "idt.h"
#include "idt.h"
#include "type.h"
void
set_idt_gate
(
int
n
,
u
32
handler
)
{
void
set_idt_gate
(
int
n
,
u
int32_t
handler
)
{
idt
[
n
].
low_offset
=
low_16
(
handler
);
idt
[
n
].
low_offset
=
low_16
(
handler
);
idt
[
n
].
sel
=
KERNEL_CS
;
idt
[
n
].
sel
=
KERNEL_CS
;
idt
[
n
].
always0
=
0
;
idt
[
n
].
always0
=
0
;
...
@@ -9,7 +10,7 @@ void set_idt_gate(int n, u32 handler) {
...
@@ -9,7 +10,7 @@ void set_idt_gate(int n, u32 handler) {
}
}
void
set_idt
()
{
void
set_idt
()
{
idt_reg
.
base
=
(
u
32
)
&
idt
;
idt_reg
.
base
=
(
u
int32_t
)
&
idt
;
idt_reg
.
limit
=
IDT_ENTRIES
*
sizeof
(
idt_gate_t
)
-
1
;
idt_reg
.
limit
=
IDT_ENTRIES
*
sizeof
(
idt_gate_t
)
-
1
;
/* Don't make the mistake of loading &idt -- always load &idt_reg */
/* Don't make the mistake of loading &idt -- always load &idt_reg */
__asm__
__volatile__
(
"lidtl (%0)"
:
:
"r"
(
&
idt_reg
));
__asm__
__volatile__
(
"lidtl (%0)"
:
:
"r"
(
&
idt_reg
));
...
...
23-fixes/cpu/idt.h
浏览文件 @
de3d4425
#ifndef IDT_H
#ifndef IDT_H
#define IDT_H
#define IDT_H
#include
"type.h"
#include
<stdint.h>
/* Segment selectors */
/* Segment selectors */
#define KERNEL_CS 0x08
#define KERNEL_CS 0x08
/* How every interrupt gate (handler) is defined */
/* How every interrupt gate (handler) is defined */
typedef
struct
{
typedef
struct
{
u
16
low_offset
;
/* Lower 16 bits of handler function address */
u
int16_t
low_offset
;
/* Lower 16 bits of handler function address */
u
16
sel
;
/* Kernel segment selector */
u
int16_t
sel
;
/* Kernel segment selector */
u
8
always0
;
u
int8_t
always0
;
/* First byte
/* First byte
* Bit 7: "Interrupt is present"
* Bit 7: "Interrupt is present"
* Bits 6-5: Privilege level of caller (0=kernel..3=user)
* Bits 6-5: Privilege level of caller (0=kernel..3=user)
* Bit 4: Set to 0 for interrupt gates
* Bit 4: Set to 0 for interrupt gates
* Bits 3-0: bits 1110 = decimal 14 = "32 bit interrupt gate" */
* Bits 3-0: bits 1110 = decimal 14 = "32 bit interrupt gate" */
u
8
flags
;
u
int8_t
flags
;
u
16
high_offset
;
/* Higher 16 bits of handler function address */
u
int16_t
high_offset
;
/* Higher 16 bits of handler function address */
}
__attribute__
((
packed
))
idt_gate_t
;
}
__attribute__
((
packed
))
idt_gate_t
;
/* A pointer to the array of interrupt handlers.
/* A pointer to the array of interrupt handlers.
* Assembly instruction 'lidt' will read it */
* Assembly instruction 'lidt' will read it */
typedef
struct
{
typedef
struct
{
u
16
limit
;
u
int16_t
limit
;
u
32
base
;
u
int32_t
base
;
}
__attribute__
((
packed
))
idt_register_t
;
}
__attribute__
((
packed
))
idt_register_t
;
#define IDT_ENTRIES 256
#define IDT_ENTRIES 256
...
@@ -33,7 +33,7 @@ idt_register_t idt_reg;
...
@@ -33,7 +33,7 @@ idt_register_t idt_reg;
/* Functions implemented in idt.c */
/* Functions implemented in idt.c */
void
set_idt_gate
(
int
n
,
u
32
handler
);
void
set_idt_gate
(
int
n
,
u
int32_t
handler
);
void
set_idt
();
void
set_idt
();
#endif
#endif
23-fixes/cpu/isr.c
浏览文件 @
de3d4425
...
@@ -11,38 +11,38 @@ isr_t interrupt_handlers[256];
...
@@ -11,38 +11,38 @@ isr_t interrupt_handlers[256];
/* Can't do this with a loop because we need the address
/* Can't do this with a loop because we need the address
* of the function names */
* of the function names */
void
isr_install
()
{
void
isr_install
()
{
set_idt_gate
(
0
,
(
u
32
)
isr0
);
set_idt_gate
(
0
,
(
u
int32_t
)
isr0
);
set_idt_gate
(
1
,
(
u
32
)
isr1
);
set_idt_gate
(
1
,
(
u
int32_t
)
isr1
);
set_idt_gate
(
2
,
(
u
32
)
isr2
);
set_idt_gate
(
2
,
(
u
int32_t
)
isr2
);
set_idt_gate
(
3
,
(
u
32
)
isr3
);
set_idt_gate
(
3
,
(
u
int32_t
)
isr3
);
set_idt_gate
(
4
,
(
u
32
)
isr4
);
set_idt_gate
(
4
,
(
u
int32_t
)
isr4
);
set_idt_gate
(
5
,
(
u
32
)
isr5
);
set_idt_gate
(
5
,
(
u
int32_t
)
isr5
);
set_idt_gate
(
6
,
(
u
32
)
isr6
);
set_idt_gate
(
6
,
(
u
int32_t
)
isr6
);
set_idt_gate
(
7
,
(
u
32
)
isr7
);
set_idt_gate
(
7
,
(
u
int32_t
)
isr7
);
set_idt_gate
(
8
,
(
u
32
)
isr8
);
set_idt_gate
(
8
,
(
u
int32_t
)
isr8
);
set_idt_gate
(
9
,
(
u
32
)
isr9
);
set_idt_gate
(
9
,
(
u
int32_t
)
isr9
);
set_idt_gate
(
10
,
(
u
32
)
isr10
);
set_idt_gate
(
10
,
(
u
int32_t
)
isr10
);
set_idt_gate
(
11
,
(
u
32
)
isr11
);
set_idt_gate
(
11
,
(
u
int32_t
)
isr11
);
set_idt_gate
(
12
,
(
u
32
)
isr12
);
set_idt_gate
(
12
,
(
u
int32_t
)
isr12
);
set_idt_gate
(
13
,
(
u
32
)
isr13
);
set_idt_gate
(
13
,
(
u
int32_t
)
isr13
);
set_idt_gate
(
14
,
(
u
32
)
isr14
);
set_idt_gate
(
14
,
(
u
int32_t
)
isr14
);
set_idt_gate
(
15
,
(
u
32
)
isr15
);
set_idt_gate
(
15
,
(
u
int32_t
)
isr15
);
set_idt_gate
(
16
,
(
u
32
)
isr16
);
set_idt_gate
(
16
,
(
u
int32_t
)
isr16
);
set_idt_gate
(
17
,
(
u
32
)
isr17
);
set_idt_gate
(
17
,
(
u
int32_t
)
isr17
);
set_idt_gate
(
18
,
(
u
32
)
isr18
);
set_idt_gate
(
18
,
(
u
int32_t
)
isr18
);
set_idt_gate
(
19
,
(
u
32
)
isr19
);
set_idt_gate
(
19
,
(
u
int32_t
)
isr19
);
set_idt_gate
(
20
,
(
u
32
)
isr20
);
set_idt_gate
(
20
,
(
u
int32_t
)
isr20
);
set_idt_gate
(
21
,
(
u
32
)
isr21
);
set_idt_gate
(
21
,
(
u
int32_t
)
isr21
);
set_idt_gate
(
22
,
(
u
32
)
isr22
);
set_idt_gate
(
22
,
(
u
int32_t
)
isr22
);
set_idt_gate
(
23
,
(
u
32
)
isr23
);
set_idt_gate
(
23
,
(
u
int32_t
)
isr23
);
set_idt_gate
(
24
,
(
u
32
)
isr24
);
set_idt_gate
(
24
,
(
u
int32_t
)
isr24
);
set_idt_gate
(
25
,
(
u
32
)
isr25
);
set_idt_gate
(
25
,
(
u
int32_t
)
isr25
);
set_idt_gate
(
26
,
(
u
32
)
isr26
);
set_idt_gate
(
26
,
(
u
int32_t
)
isr26
);
set_idt_gate
(
27
,
(
u
32
)
isr27
);
set_idt_gate
(
27
,
(
u
int32_t
)
isr27
);
set_idt_gate
(
28
,
(
u
32
)
isr28
);
set_idt_gate
(
28
,
(
u
int32_t
)
isr28
);
set_idt_gate
(
29
,
(
u
32
)
isr29
);
set_idt_gate
(
29
,
(
u
int32_t
)
isr29
);
set_idt_gate
(
30
,
(
u
32
)
isr30
);
set_idt_gate
(
30
,
(
u
int32_t
)
isr30
);
set_idt_gate
(
31
,
(
u
32
)
isr31
);
set_idt_gate
(
31
,
(
u
int32_t
)
isr31
);
// Remap the PIC
// Remap the PIC
port_byte_out
(
0x20
,
0x11
);
port_byte_out
(
0x20
,
0x11
);
...
@@ -57,22 +57,22 @@ void isr_install() {
...
@@ -57,22 +57,22 @@ void isr_install() {
port_byte_out
(
0xA1
,
0x0
);
port_byte_out
(
0xA1
,
0x0
);
// Install the IRQs
// Install the IRQs
set_idt_gate
(
32
,
(
u
32
)
irq0
);
set_idt_gate
(
32
,
(
u
int32_t
)
irq0
);
set_idt_gate
(
33
,
(
u
32
)
irq1
);
set_idt_gate
(
33
,
(
u
int32_t
)
irq1
);
set_idt_gate
(
34
,
(
u
32
)
irq2
);
set_idt_gate
(
34
,
(
u
int32_t
)
irq2
);
set_idt_gate
(
35
,
(
u
32
)
irq3
);
set_idt_gate
(
35
,
(
u
int32_t
)
irq3
);
set_idt_gate
(
36
,
(
u
32
)
irq4
);
set_idt_gate
(
36
,
(
u
int32_t
)
irq4
);
set_idt_gate
(
37
,
(
u
32
)
irq5
);
set_idt_gate
(
37
,
(
u
int32_t
)
irq5
);
set_idt_gate
(
38
,
(
u
32
)
irq6
);
set_idt_gate
(
38
,
(
u
int32_t
)
irq6
);
set_idt_gate
(
39
,
(
u
32
)
irq7
);
set_idt_gate
(
39
,
(
u
int32_t
)
irq7
);
set_idt_gate
(
40
,
(
u
32
)
irq8
);
set_idt_gate
(
40
,
(
u
int32_t
)
irq8
);
set_idt_gate
(
41
,
(
u
32
)
irq9
);
set_idt_gate
(
41
,
(
u
int32_t
)
irq9
);
set_idt_gate
(
42
,
(
u
32
)
irq10
);
set_idt_gate
(
42
,
(
u
int32_t
)
irq10
);
set_idt_gate
(
43
,
(
u
32
)
irq11
);
set_idt_gate
(
43
,
(
u
int32_t
)
irq11
);
set_idt_gate
(
44
,
(
u
32
)
irq12
);
set_idt_gate
(
44
,
(
u
int32_t
)
irq12
);
set_idt_gate
(
45
,
(
u
32
)
irq13
);
set_idt_gate
(
45
,
(
u
int32_t
)
irq13
);
set_idt_gate
(
46
,
(
u
32
)
irq14
);
set_idt_gate
(
46
,
(
u
int32_t
)
irq14
);
set_idt_gate
(
47
,
(
u
32
)
irq15
);
set_idt_gate
(
47
,
(
u
int32_t
)
irq15
);
set_idt
();
// Load with ASM
set_idt
();
// Load with ASM
}
}
...
@@ -126,7 +126,7 @@ void isr_handler(registers_t r) {
...
@@ -126,7 +126,7 @@ void isr_handler(registers_t r) {
kprint
(
"
\n
"
);
kprint
(
"
\n
"
);
}
}
void
register_interrupt_handler
(
u
8
n
,
isr_t
handler
)
{
void
register_interrupt_handler
(
u
int8_t
n
,
isr_t
handler
)
{
interrupt_handlers
[
n
]
=
handler
;
interrupt_handlers
[
n
]
=
handler
;
}
}
...
...
23-fixes/cpu/isr.h
浏览文件 @
de3d4425
#ifndef ISR_H
#ifndef ISR_H
#define ISR_H
#define ISR_H
#include
"type.h"
#include
<stdint.h>
/* ISRs reserved for CPU exceptions */
/* ISRs reserved for CPU exceptions */
extern
void
isr0
();
extern
void
isr0
();
...
@@ -73,10 +73,10 @@ extern void irq15();
...
@@ -73,10 +73,10 @@ extern void irq15();
/* Struct which aggregates many registers */
/* Struct which aggregates many registers */
typedef
struct
{
typedef
struct
{
u
32
ds
;
/* Data segment selector */
u
int32_t
ds
;
/* Data segment selector */
u
32
edi
,
esi
,
ebp
,
esp
,
ebx
,
edx
,
ecx
,
eax
;
/* Pushed by pusha. */
u
int32_t
edi
,
esi
,
ebp
,
esp
,
ebx
,
edx
,
ecx
,
eax
;
/* Pushed by pusha. */
u
32
int_no
,
err_code
;
/* Interrupt number and error code (if applicable) */
u
int32_t
int_no
,
err_code
;
/* Interrupt number and error code (if applicable) */
u
32
eip
,
cs
,
eflags
,
useresp
,
ss
;
/* Pushed by the processor automatically */
u
int32_t
eip
,
cs
,
eflags
,
useresp
,
ss
;
/* Pushed by the processor automatically */
}
registers_t
;
}
registers_t
;
void
isr_install
();
void
isr_install
();
...
@@ -84,6 +84,6 @@ void isr_handler(registers_t r);
...
@@ -84,6 +84,6 @@ void isr_handler(registers_t r);
void
irq_install
();
void
irq_install
();
typedef
void
(
*
isr_t
)(
registers_t
);
typedef
void
(
*
isr_t
)(
registers_t
);
void
register_interrupt_handler
(
u
8
n
,
isr_t
handler
);
void
register_interrupt_handler
(
u
int8_t
n
,
isr_t
handler
);
#endif
#endif
23-fixes/cpu/ports.c
浏览文件 @
de3d4425
...
@@ -3,8 +3,8 @@
...
@@ -3,8 +3,8 @@
/**
/**
* Read a byte from the specified port
* Read a byte from the specified port
*/
*/
u
8
port_byte_in
(
u16
port
)
{
u
int8_t
port_byte_in
(
uint16_t
port
)
{
u
8
result
;
u
int8_t
result
;
/* Inline assembler syntax
/* Inline assembler syntax
* !! Notice how the source and destination registers are switched from NASM !!
* !! Notice how the source and destination registers are switched from NASM !!
*
*
...
@@ -17,7 +17,7 @@ u8 port_byte_in (u16 port) {
...
@@ -17,7 +17,7 @@ u8 port_byte_in (u16 port) {
return
result
;
return
result
;
}
}
void
port_byte_out
(
u
16
port
,
u8
data
)
{
void
port_byte_out
(
u
int16_t
port
,
uint8_t
data
)
{
/* Notice how here both registers are mapped to C variables and
/* Notice how here both registers are mapped to C variables and
* nothing is returned, thus, no equals '=' in the asm syntax
* nothing is returned, thus, no equals '=' in the asm syntax
* However we see a comma since there are two variables in the input area
* However we see a comma since there are two variables in the input area
...
@@ -26,12 +26,12 @@ void port_byte_out (u16 port, u8 data) {
...
@@ -26,12 +26,12 @@ void port_byte_out (u16 port, u8 data) {
__asm__
__volatile__
(
"out %%al, %%dx"
:
:
"a"
(
data
),
"d"
(
port
));
__asm__
__volatile__
(
"out %%al, %%dx"
:
:
"a"
(
data
),
"d"
(
port
));
}
}
u
16
port_word_in
(
u16
port
)
{
u
int16_t
port_word_in
(
uint16_t
port
)
{
u
16
result
;
u
int16_t
result
;
__asm__
(
"in %%dx, %%ax"
:
"=a"
(
result
)
:
"d"
(
port
));
__asm__
(
"in %%dx, %%ax"
:
"=a"
(
result
)
:
"d"
(
port
));
return
result
;
return
result
;
}
}
void
port_word_out
(
u
16
port
,
u16
data
)
{
void
port_word_out
(
u
int16_t
port
,
uint16_t
data
)
{
__asm__
__volatile__
(
"out %%ax, %%dx"
:
:
"a"
(
data
),
"d"
(
port
));
__asm__
__volatile__
(
"out %%ax, %%dx"
:
:
"a"
(
data
),
"d"
(
port
));
}
}
23-fixes/cpu/ports.h
浏览文件 @
de3d4425
#ifndef PORTS_H
#ifndef PORTS_H
#define PORTS_H
#define PORTS_H
#include
"../cpu/type.h"
#include
<stdint.h>
unsigned
char
port_byte_in
(
u
16
port
);
unsigned
char
port_byte_in
(
u
int16_t
port
);
void
port_byte_out
(
u
16
port
,
u8
data
);
void
port_byte_out
(
u
int16_t
port
,
uint8_t
data
);
unsigned
short
port_word_in
(
u
16
port
);
unsigned
short
port_word_in
(
u
int16_t
port
);
void
port_word_out
(
u
16
port
,
u16
data
);
void
port_word_out
(
u
int16_t
port
,
uint16_t
data
);
#endif
#endif
23-fixes/cpu/timer.c
浏览文件 @
de3d4425
...
@@ -3,21 +3,21 @@
...
@@ -3,21 +3,21 @@
#include "ports.h"
#include "ports.h"
#include "../libc/function.h"
#include "../libc/function.h"
u
32
tick
=
0
;
u
int32_t
tick
=
0
;
static
void
timer_callback
(
registers_t
regs
)
{
static
void
timer_callback
(
registers_t
regs
)
{
tick
++
;
tick
++
;
UNUSED
(
regs
);
UNUSED
(
regs
);
}
}
void
init_timer
(
u
32
freq
)
{
void
init_timer
(
u
int32_t
freq
)
{
/* Install the function we just wrote */
/* Install the function we just wrote */
register_interrupt_handler
(
IRQ0
,
timer_callback
);
register_interrupt_handler
(
IRQ0
,
timer_callback
);
/* Get the PIT value: hardware clock at 1193180 Hz */
/* Get the PIT value: hardware clock at 1193180 Hz */
u
32
divisor
=
1193180
/
freq
;
u
int32_t
divisor
=
1193180
/
freq
;
u
8
low
=
(
u8
)(
divisor
&
0xFF
);
u
int8_t
low
=
(
uint8_t
)(
divisor
&
0xFF
);
u
8
high
=
(
u8
)(
(
divisor
>>
8
)
&
0xFF
);
u
int8_t
high
=
(
uint8_t
)(
(
divisor
>>
8
)
&
0xFF
);
/* Send the command */
/* Send the command */
port_byte_out
(
0x43
,
0x36
);
/* Command port */
port_byte_out
(
0x43
,
0x36
);
/* Command port */
port_byte_out
(
0x40
,
low
);
port_byte_out
(
0x40
,
low
);
...
...
23-fixes/cpu/timer.h
浏览文件 @
de3d4425
#ifndef TIMER_H
#ifndef TIMER_H
#define TIMER_H
#define TIMER_H
#include
"type.h"
#include
<stdint.h>
void
init_timer
(
u
32
freq
);
void
init_timer
(
u
int32_t
freq
);
#endif
#endif
23-fixes/cpu/type.h
浏览文件 @
de3d4425
#ifndef TYPE_H
#ifndef TYPE_H
#define TYPE_H
#define TYPE_H
/* Instead of using 'chars' to allocate non-character bytes,
#include <stdint.h>
* we will use these new type with no semantic meaning */
typedef
unsigned
int
u32
;
typedef
int
s32
;
typedef
unsigned
short
u16
;
typedef
short
s16
;
typedef
unsigned
char
u8
;
typedef
char
s8
;
#define low_16(address) (u
16
)((address) & 0xFFFF)
#define low_16(address) (u
int16_t
)((address) & 0xFFFF)
#define high_16(address) (u
16
)(((address) >> 16) & 0xFFFF)
#define high_16(address) (u
int16_t
)(((address) >> 16) & 0xFFFF)
#endif
#endif
23-fixes/drivers/keyboard.c
浏览文件 @
de3d4425
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
#include "../libc/string.h"
#include "../libc/string.h"
#include "../libc/function.h"
#include "../libc/function.h"
#include "../kernel/kernel.h"
#include "../kernel/kernel.h"
#include <stdint.h>
#define BACKSPACE 0x0E
#define BACKSPACE 0x0E
#define ENTER 0x1C
#define ENTER 0x1C
...
@@ -26,7 +27,7 @@ const char sc_ascii[] = { '?', '?', '1', '2', '3', '4', '5', '6',
...
@@ -26,7 +27,7 @@ const char sc_ascii[] = { '?', '?', '1', '2', '3', '4', '5', '6',
static
void
keyboard_callback
(
registers_t
regs
)
{
static
void
keyboard_callback
(
registers_t
regs
)
{
/* The PIC leaves us the scancode in port 0x60 */
/* The PIC leaves us the scancode in port 0x60 */
u
8
scancode
=
port_byte_in
(
0x60
);
u
int8_t
scancode
=
port_byte_in
(
0x60
);
if
(
scancode
>
SC_MAX
)
return
;
if
(
scancode
>
SC_MAX
)
return
;
if
(
scancode
==
BACKSPACE
)
{
if
(
scancode
==
BACKSPACE
)
{
...
...
23-fixes/drivers/keyboard.h
浏览文件 @
de3d4425
#include "../cpu/type.h"
void
init_keyboard
();
void
init_keyboard
();
23-fixes/drivers/screen.c
浏览文件 @
de3d4425
#include "screen.h"
#include "screen.h"
#include "../cpu/ports.h"
#include "../cpu/ports.h"
#include "../libc/mem.h"
#include "../libc/mem.h"
#include <stdint.h>
/* Declaration of private functions */
/* Declaration of private functions */
int
get_cursor_offset
();
int
get_cursor_offset
();
...
@@ -65,7 +66,7 @@ void kprint_backspace() {
...
@@ -65,7 +66,7 @@ void kprint_backspace() {
* Sets the video cursor to the returned offset
* Sets the video cursor to the returned offset
*/
*/
int
print_char
(
char
c
,
int
col
,
int
row
,
char
attr
)
{
int
print_char
(
char
c
,
int
col
,
int
row
,
char
attr
)
{
u
8
*
vidmem
=
(
u8
*
)
VIDEO_ADDRESS
;
u
int8_t
*
vidmem
=
(
uint8_t
*
)
VIDEO_ADDRESS
;
if
(
!
attr
)
attr
=
WHITE_ON_BLACK
;
if
(
!
attr
)
attr
=
WHITE_ON_BLACK
;
/* Error control: print a red 'E' if the coords aren't right */
/* Error control: print a red 'E' if the coords aren't right */
...
@@ -95,12 +96,12 @@ int print_char(char c, int col, int row, char attr) {
...
@@ -95,12 +96,12 @@ int print_char(char c, int col, int row, char attr) {
if
(
offset
>=
MAX_ROWS
*
MAX_COLS
*
2
)
{
if
(
offset
>=
MAX_ROWS
*
MAX_COLS
*
2
)
{
int
i
;
int
i
;
for
(
i
=
1
;
i
<
MAX_ROWS
;
i
++
)
for
(
i
=
1
;
i
<
MAX_ROWS
;
i
++
)
memory_copy
((
u
8
*
)(
get_offset
(
0
,
i
)
+
VIDEO_ADDRESS
),
memory_copy
((
u
int8_t
*
)(
get_offset
(
0
,
i
)
+
VIDEO_ADDRESS
),
(
u
8
*
)(
get_offset
(
0
,
i
-
1
)
+
VIDEO_ADDRESS
),
(
u
int8_t
*
)(
get_offset
(
0
,
i
-
1
)
+
VIDEO_ADDRESS
),
MAX_COLS
*
2
);
MAX_COLS
*
2
);
/* Blank last line */
/* Blank last line */
char
*
last_line
=
(
char
*
)
(
get_offset
(
0
,
MAX_ROWS
-
1
)
+
(
u
8
*
)
VIDEO_ADDRESS
);
char
*
last_line
=
(
char
*
)
(
get_offset
(
0
,
MAX_ROWS
-
1
)
+
(
u
int8_t
*
)
VIDEO_ADDRESS
);
for
(
i
=
0
;
i
<
MAX_COLS
*
2
;
i
++
)
last_line
[
i
]
=
0
;
for
(
i
=
0
;
i
<
MAX_COLS
*
2
;
i
++
)
last_line
[
i
]
=
0
;
offset
-=
2
*
MAX_COLS
;
offset
-=
2
*
MAX_COLS
;
...
@@ -126,15 +127,15 @@ void set_cursor_offset(int offset) {
...
@@ -126,15 +127,15 @@ void set_cursor_offset(int offset) {
/* Similar to get_cursor_offset, but instead of reading we write data */
/* Similar to get_cursor_offset, but instead of reading we write data */
offset
/=
2
;
offset
/=
2
;
port_byte_out
(
REG_SCREEN_CTRL
,
14
);
port_byte_out
(
REG_SCREEN_CTRL
,
14
);
port_byte_out
(
REG_SCREEN_DATA
,
(
u
8
)(
offset
>>
8
));
port_byte_out
(
REG_SCREEN_DATA
,
(
u
int8_t
)(
offset
>>
8
));
port_byte_out
(
REG_SCREEN_CTRL
,
15
);
port_byte_out
(
REG_SCREEN_CTRL
,
15
);
port_byte_out
(
REG_SCREEN_DATA
,
(
u
8
)(
offset
&
0xff
));
port_byte_out
(
REG_SCREEN_DATA
,
(
u
int8_t
)(
offset
&
0xff
));
}
}
void
clear_screen
()
{
void
clear_screen
()
{
int
screen_size
=
MAX_COLS
*
MAX_ROWS
;
int
screen_size
=
MAX_COLS
*
MAX_ROWS
;
int
i
;
int
i
;
u
8
*
screen
=
(
u8
*
)
VIDEO_ADDRESS
;
u
int8_t
*
screen
=
(
uint8_t
*
)
VIDEO_ADDRESS
;
for
(
i
=
0
;
i
<
screen_size
;
i
++
)
{
for
(
i
=
0
;
i
<
screen_size
;
i
++
)
{
screen
[
i
*
2
]
=
' '
;
screen
[
i
*
2
]
=
' '
;
...
...
23-fixes/drivers/screen.h
浏览文件 @
de3d4425
#ifndef SCREEN_H
#ifndef SCREEN_H
#define SCREEN_H
#define SCREEN_H
#include "../cpu/type.h"
#define VIDEO_ADDRESS 0xb8000
#define VIDEO_ADDRESS 0xb8000
#define MAX_ROWS 25
#define MAX_ROWS 25
#define MAX_COLS 80
#define MAX_COLS 80
...
...
23-fixes/kernel/kernel.c
浏览文件 @
de3d4425
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include "kernel.h"
#include "kernel.h"
#include "../libc/string.h"
#include "../libc/string.h"
#include "../libc/mem.h"
#include "../libc/mem.h"
#include <stdint.h>
void
kernel_main
()
{
void
kernel_main
()
{
isr_install
();
isr_install
();
...
@@ -18,8 +19,8 @@ void user_input(char *input) {
...
@@ -18,8 +19,8 @@ void user_input(char *input) {
asm
volatile
(
"hlt"
);
asm
volatile
(
"hlt"
);
}
else
if
(
strcmp
(
input
,
"PAGE"
)
==
0
)
{
}
else
if
(
strcmp
(
input
,
"PAGE"
)
==
0
)
{
/* Lesson 22: Code to test kmalloc, the rest is unchanged */
/* Lesson 22: Code to test kmalloc, the rest is unchanged */
u
32
phys_addr
;
u
int32_t
phys_addr
;
u
32
page
=
kmalloc
(
1000
,
1
,
&
phys_addr
);
u
int32_t
page
=
kmalloc
(
1000
,
1
,
&
phys_addr
);
char
page_str
[
16
]
=
""
;
char
page_str
[
16
]
=
""
;
hex_to_ascii
(
page
,
page_str
);
hex_to_ascii
(
page
,
page_str
);
char
phys_str
[
16
]
=
""
;
char
phys_str
[
16
]
=
""
;
...
...
23-fixes/libc/mem.c
浏览文件 @
de3d4425
#include "mem.h"
#include "mem.h"
void
memory_copy
(
u
8
*
source
,
u8
*
dest
,
int
nbytes
)
{
void
memory_copy
(
u
int8_t
*
source
,
uint8_t
*
dest
,
int
nbytes
)
{
int
i
;
int
i
;
for
(
i
=
0
;
i
<
nbytes
;
i
++
)
{
for
(
i
=
0
;
i
<
nbytes
;
i
++
)
{
*
(
dest
+
i
)
=
*
(
source
+
i
);
*
(
dest
+
i
)
=
*
(
source
+
i
);
}
}
}
}
void
memory_set
(
u
8
*
dest
,
u8
val
,
u32
len
)
{
void
memory_set
(
u
int8_t
*
dest
,
uint8_t
val
,
uint32_t
len
)
{
u
8
*
temp
=
(
u8
*
)
dest
;
u
int8_t
*
temp
=
(
uint8_t
*
)
dest
;
for
(
;
len
!=
0
;
len
--
)
*
temp
++
=
val
;
for
(
;
len
!=
0
;
len
--
)
*
temp
++
=
val
;
}
}
/* This should be computed at link time, but a hardcoded
/* This should be computed at link time, but a hardcoded
* value is fine for now. Remember that our kernel starts
* value is fine for now. Remember that our kernel starts
* at 0x1000 as defined on the Makefile */
* at 0x1000 as defined on the Makefile */
u
32
free_mem_addr
=
0x10000
;
u
int32_t
free_mem_addr
=
0x10000
;
/* Implementation is just a pointer to some free memory which
/* Implementation is just a pointer to some free memory which
* keeps growing */
* keeps growing */
u
32
kmalloc
(
u32
size
,
int
align
,
u32
*
phys_addr
)
{
u
int32_t
kmalloc
(
uint32_t
size
,
int
align
,
uint32_t
*
phys_addr
)
{
/* Pages are aligned to 4K, or 0x1000 */
/* Pages are aligned to 4K, or 0x1000 */
if
(
align
==
1
&&
(
free_mem_addr
&
0xFFFFF000
))
{
if
(
align
==
1
&&
(
free_mem_addr
&
0xFFFFF000
))
{
free_mem_addr
&=
0xFFFFF000
;
free_mem_addr
&=
0xFFFFF000
;
...
@@ -27,7 +27,7 @@ u32 kmalloc(u32 size, int align, u32 *phys_addr) {
...
@@ -27,7 +27,7 @@ u32 kmalloc(u32 size, int align, u32 *phys_addr) {
/* Save also the physical address */
/* Save also the physical address */
if
(
phys_addr
)
*
phys_addr
=
free_mem_addr
;
if
(
phys_addr
)
*
phys_addr
=
free_mem_addr
;
u
32
ret
=
free_mem_addr
;
u
int32_t
ret
=
free_mem_addr
;
free_mem_addr
+=
size
;
/* Remember to increment the pointer */
free_mem_addr
+=
size
;
/* Remember to increment the pointer */
return
ret
;
return
ret
;
}
}
23-fixes/libc/mem.h
浏览文件 @
de3d4425
#ifndef MEM_H
#ifndef MEM_H
#define MEM_H
#define MEM_H
#include
"../cpu/type.h"
#include
<stdint.h>
void
memory_copy
(
u
8
*
source
,
u8
*
dest
,
int
nbytes
);
void
memory_copy
(
u
int8_t
*
source
,
uint8_t
*
dest
,
int
nbytes
);
void
memory_set
(
u
8
*
dest
,
u8
val
,
u32
len
);
void
memory_set
(
u
int8_t
*
dest
,
uint8_t
val
,
uint32_t
len
);
/* At this stage there is no 'free' implemented. */
/* At this stage there is no 'free' implemented. */
u
32
kmalloc
(
u32
size
,
int
align
,
u32
*
phys_addr
);
u
int32_t
kmalloc
(
uint32_t
size
,
int
align
,
uint32_t
*
phys_addr
);
#endif
#endif
23-fixes/libc/string.c
浏览文件 @
de3d4425
#include "string.h"
#include "string.h"
#include
"../cpu/type.h"
#include
<stdint.h>
/**
/**
* K&R implementation
* K&R implementation
...
@@ -23,7 +23,7 @@ void hex_to_ascii(int n, char str[]) {
...
@@ -23,7 +23,7 @@ void hex_to_ascii(int n, char str[]) {
append
(
str
,
'x'
);
append
(
str
,
'x'
);
char
zeros
=
0
;
char
zeros
=
0
;
s32
tmp
;
int32_t
tmp
;
int
i
;
int
i
;
for
(
i
=
28
;
i
>
0
;
i
-=
4
)
{
for
(
i
=
28
;
i
>
0
;
i
-=
4
)
{
tmp
=
(
n
>>
i
)
&
0xF
;
tmp
=
(
n
>>
i
)
&
0xF
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录