0x06_自制操作系统My-OS,IDT,GDT,PIC初始化,实现键盘中断
把class03改成class04
IDT,GDT,PIC
我来介绍什么是IDT和GDT,PIC,怎么实现键盘中断
GDT全局描述表在16位CPU用不到,到了32位CPU要用。
16位CPU实模式用基地址x16+偏移地址去寻找内存地址,到了32位也用基地址x16+偏移地址找地址但是,32位CPU有保护模式,防止程序乱访问内存
所以用GDT
IDT,Interrupt Descriptor Table,即中断描述符表,用来联系中断和处理函数的,比如A中断发生->执行B函数
PIC(可编程中断控制器),中断是由PIC产生的
这是我自己的理解,你要想搞明白得自己去找资料
实现键盘中断
键盘按下-PIC收到发起中断-CPU处理中断-执行对应函数
这是我自己意淫的,具体怎么回事你得自己找资料
首先初始化pic,gdt,idt
创建文件dsctbl.c
- /* GDT、IDT、descriptor table 关系处理 */
- #include "include/head.h"
- void init_gdtidt(void)
- {
- struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT;
- struct GATE_DESCRIPTOR *idt = (struct GATE_DESCRIPTOR *) ADR_IDT;
- int i;
- /* GDT初始化 */
- for (i = 0; i <= LIMIT_GDT / 8; i++) {
- set_segmdesc(gdt + i, 0, 0, 0);
- }
- set_segmdesc(gdt + 1, 0xffffffff, 0x00000000, AR_DATA32_RW);
- set_segmdesc(gdt + 2, LIMIT_BOTPAK, ADR_BOTPAK, AR_CODE32_ER);
- load_gdtr(LIMIT_GDT, ADR_GDT);
- /* IDT初始化 */
- for (i = 0; i <= LIMIT_IDT / 8; i++) {
- set_gatedesc(idt + i, 0, 0, 0);
- }
- load_idtr(LIMIT_IDT, ADR_IDT);
- /* IDT设置*/ set_gatedesc(idt + 0x21, (int) asm_inthandler21, 2 * 8, AR_INTGATE32);
- /* IDT的设定 */
- return;
- }
- void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar)
- {
- if (limit > 0xfffff) {
- ar |= 0x8000; /* G_bit = 1 */
- limit /= 0x1000;
- }
- sd->limit_low = limit & 0xffff;
- sd->base_low = base & 0xffff;
- sd->base_mid = (base >> 16) & 0xff;
- sd->access_right = ar & 0xff;
- sd->limit_high = ((limit >> 16) & 0x0f) | ((ar >> 8) & 0xf0);
- sd->base_high = (base >> 24) & 0xff;
- return;
- }
- void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar)
- {
- gd->offset_low = offset & 0xffff;
- gd->selector = selector;
- gd->dw_count = (ar >> 8) & 0xff;
- gd->access_right = ar & 0xff;
- gd->offset_high = (offset >> 16) & 0xffff;
- return;
- }
加粗的地方就是注册21号键盘中断处理函数到IDT
head.h
- /*naskfunc.asm*/
- void io_stihlt();
- void io_hlt(void);
- void io_cli(void);
- void io_sti(void);
- int io_get8(int port);
- void io_set8(int port, int data);
- void write_mem8(int addr, int data);
- int io_load_eflags(void);
- void io_store_eflags(int eflags);
- void asm_inthandler21(void);
- /* asmhead.nas */
- struct BOOTINFO { /* 0x0ff0-0x0fff */
- char cyls; /* 启动区读磁盘读到此为止 */
- char leds; /* 启动时键盘的LED的状态 */
- char vmode; /* 显卡模式为多少位彩色 */
- char reserve;
- short scrnx, scrny; /* 画面分辨率 */
- char *vram;
- };
- #define ADR_BOOTINFO 0x00000ff0
- /*graphic.c*/
- void init_palette(void);
- void set_palette(int start, int end, unsigned char *rgb);
- void boxfill8(unsigned char *vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1);
- void putfont8(char *vram, int xsize, int x, int y, char c, char *font);
- void putfonts8_asc(char *vram, int xsize, int x, int y, char c, unsigned char *s);
- /*font*/
- extern char font[4096];
- /* dsctbl.c */
- struct SEGMENT_DESCRIPTOR {
- short limit_low, base_low;
- char base_mid, access_right;
- char limit_high, base_high;
- };
- struct GATE_DESCRIPTOR {
- short offset_low, selector;
- char dw_count, access_right;
- short offset_high;
- };
- void init_gdtidt(void);
- void set_segmdesc(struct SEGMENT_DESCRIPTOR *sd, unsigned int limit, int base, int ar);
- void set_gatedesc(struct GATE_DESCRIPTOR *gd, int offset, int selector, int ar);
- #define ADR_IDT 0x0026f800
- #define LIMIT_IDT 0x000007ff
- #define ADR_GDT 0x00270000
- #define LIMIT_GDT 0x0000ffff
- #define ADR_BOTPAK 0x00280000
- #define LIMIT_BOTPAK 0x0007ffff
- #define AR_DATA32_RW 0x4092
- #define AR_CODE32_ER 0x409a
- #define AR_INTGATE32 0x008e
- /* pic.c */
- void init_pic(void);
- #define PIC0_ICW1 0x0020
- #define PIC0_OCW2 0x0020
- #define PIC0_IMR 0x0021
- #define PIC0_ICW2 0x0021
- #define PIC0_ICW3 0x0021
- #define PIC0_ICW4 0x0021
- #define PIC1_ICW1 0x00a0
- #define PIC1_OCW2 0x00a0
- #define PIC1_IMR 0x00a1
- #define PIC1_ICW2 0x00a1
- #define PIC1_ICW3 0x00a1
- #define PIC1_ICW4 0x00a1
加粗的地方就是增加的地方
naskfunc.asm
- ; naskfunc
- ; TAB=4
- [FORMAT "WCOFF"] ; 制作目标文件的模式
- [INSTRSET "i486p"] ; 使用到486为止的指令
- [BITS 32] ; 3制作32位模式用的机器语言
- [FILE "naskfunc.asm"] ; 文件名
- GLOBAL _io_hlt,_write_mem8,_io_cli,_io_sti,_io_get8,_io_set8,_io_stihlt
- GLOBAL _io_load_eflags,_io_store_eflags,_asm_inthandler21
- GLOBAL _load_gdtr, _load_idtr
- EXTERN _inthandler21
- [SECTION .text]
- _io_hlt: ; void io_hlt(void);
- HLT
- RET
- _io_cli: ; void io_cli(void);
- CLI
- RET
- _io_sti: ; void io_sti(void);
- STI
- RET
- _io_get8: ; int io_get8(int port);
- MOV EDX,[ESP+4] ; port
- MOV EAX,0
- IN AL,DX
- RET
- _io_set8: ; void io_set8(int port, int data);
- MOV EDX,[ESP+4] ; port
- MOV AL,[ESP+8] ; data
- OUT DX,AL
- RET
- _io_stihlt: ; void io_stihlt(void);
- STI
- HLT
- RET
- _write_mem8: ; void write_mem8(int addr, int data);
- MOV ECX,[ESP+4] ; taking content of add
- MOV AL,[ESP+8] ; taking content of data
- MOV [ECX],AL ; *ecx=al
- RET
- _io_load_eflags: ; int io_load_eflags(void);
- PUSHFD ; PUSH EFLAGS
- POP EAX
- RET
- _io_store_eflags: ; void io_store_eflags(int eflags);
- MOV EAX,[ESP+4]
- PUSH EAX
- POPFD ; POP EFLAGS
- RET
- _load_gdtr: ; void load_gdtr(int limit, int addr);
- MOV AX,[ESP+4] ; limit
- MOV [ESP+6],AX
- LGDT [ESP+6]
- RET
- _load_idtr: ; void load_idtr(int limit, int addr);
- MOV AX,[ESP+4] ; limit
- MOV [ESP+6],AX
- LIDT [ESP+6]
- RET
- _asm_inthandler21:
- PUSH ES
- PUSH DS
- PUSHAD
- MOV EAX,ESP
- PUSH EAX
- MOV AX,SS
- MOV DS,AX
- MOV ES,AX
- CALL _inthandler21
- POP EAX
- POPAD
- POP DS
- POP ES
- IRETD
加粗的地方就是要修改的地方
IDT-->_asm_inthandler21-->_inthandler21函数
新建pic.c
- /*初始化关系 */
- #include "include/head.h"
- void init_pic(void)
- /* PIC初始化 */
- {
- io_set8(PIC0_IMR, 0xff ); /* 禁止所有中断 */
- io_set8(PIC1_IMR, 0xff ); /* 禁止所有中断 */
- io_set8(PIC0_ICW1, 0x11 ); /* 边缘触发模式(edge trigger mode) */
- io_set8(PIC0_ICW2, 0x20 ); /* IRQ0-7由INT20-27接收 */
- io_set8(PIC0_ICW3, 1 << 2); /* PIC1由IRQ2相连 */
- io_set8(PIC0_ICW4, 0x01 ); /* 无缓冲区模式 */
- io_set8(PIC1_ICW1, 0x11 ); /* 边缘触发模式(edge trigger mode) */
- io_set8(PIC1_ICW2, 0x28 ); /* IRQ8-15由INT28-2f接收 */
- io_set8(PIC1_ICW3, 2 ); /* PIC1由IRQ2连接 */
- io_set8(PIC1_ICW4, 0x01 ); /* 无缓冲区模式 */
- io_set8(PIC0_IMR, 0xfb ); /* 11111011 PIC1以外全部禁止 */
- io_set8(PIC1_IMR, 0xff ); /* 11111111 禁止所有中断 */
- return;
- }
创建interrupt.c
- #include "include/head.h"
- void inthandler21(int *esp)
- /* 来自PS/2键盘的中断 */
- {
- struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
- boxfill8(binfo->vram, binfo->scrnx, 7, 0, 0, 32 * 8 - 1, 15);
- putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, 0, "INT 21 (IRQ-1) :PS/2 keyboard");
- for (;;) {
- io_hlt();
- }
- }
最后修改mian.c
- #include "include/head.h"
- #include <string.h>
- struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
- void Main(void){
- int i;char s[256];
- init_gdtidt();
- init_pic();
- io_sti();
- init_palette();
- boxfill8(binfo->vram, binfo->scrnx, 0,0,0,binfo->scrnx, binfo->scrny);
- sprintf(s, "scrnx = %d", binfo->scrnx);
- putfonts8_asc(binfo->vram, binfo->scrnx, 8, 8, 7, s);
- io_set8(PIC0_IMR, 0xf9); /* 开放PIC1和键盘中断(11111001) */
- for (;;) {
- io_hlt();
- }
- }
别忘了编译之改一下makefile
运行:
- cd class02
- ..\z_tools\make.exe run
0x06_自制操作系统My-OS,IDT,GDT,PIC初始化,实现键盘中断的更多相关文章
- [自制简单操作系统] 2、鼠标及键盘中断处理事件[PIC\GDT\IDT\FIFO]
1.大致介绍: >_<" 大致执行顺序是:ipl10.nas->asmhead.nas->bootpack.c PS: 这里bootpack.c要调用graphic. ...
- 《30天自制操作系统》读书笔记(5) GDT&IDT
梳理项目结构 项目做到现在, 前头的好多东西都忘了, 还是通过Makefile重新理解一下整个项目是如何编译的: 现在我们拥有这么9个文件: ipl10.nas InitialProgramLo ...
- 《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!
<30天自制操作系统>笔记(01)——hello bitzhuwei's OS! 最初的OS代码 ; hello-os ; TAB=4 ORG 0x7c00 ; 指明程序的装载地址 ; 以 ...
- 《30天自制操作系统》笔记(01)——hello bitzhuwei’s OS!【转】
转自:http://www.cnblogs.com/bitzhuwei/p/OS-in-30-days-01-hello-bitzhuwei-OS.html 阅读目录(Content) 最初的OS代码 ...
- 《30天自制操作系统》笔记(06)——CPU的32位模式
<30天自制操作系统>笔记(06)——CPU的32位模式 进度回顾 上一篇中实现了启用鼠标.键盘的功能.屏幕上会显示出用户按键.点击鼠标的情况.这是通过设置硬件的中断函数实现的,可以说硬件 ...
- 《30天自制操作系统》笔记(02)——导入C语言
<30天自制操作系统>笔记(02)——导入C语言 进度回顾 在上一篇,记录了计算机开机时加载IPL程序(initial program loader,一个nas汇编程序)的情况,包括IPL ...
- 自制操作系统 (三) 从启动区执行操作系统并进入C世界
qq:992591601 欢迎交流 2016.04.03 2016.05.31 2016.06.29 这一章是有些复杂的,我不太懂作者为什么要把这么多内容都放进一天. 1读入了十个柱面 2从启动区执行 ...
- 《30天自制操作系统》12_day_学习笔记
harib09a: 定时器:(Timer)每隔一段时间,会向CPU发送一个中断.这样CPU不用记住每一条指令的执行时间.没有定时器很多指令CPU都很难执行.例如HLT指令,这个指令的执行时间不是个固定 ...
- 《30天自制操作系统》笔记(03)——使用Vmware
<30天自制操作系统>笔记(03)——使用Vmware 进度回顾 在上一篇,实现了用IPL加载OS程序到内存,然后JMP到OS程序这一功能:并且总结出下一步的OS开发结构.但是遇到了真机测 ...
- 《30天自制操作系统》06_day_学习笔记
harib03a: 内容没有变化 :P109 从这里开始,代码开始工程化了. 将原本300多行的bootpack.c分割成了三部分: graphic.c : 用来处理界面图像 dsctbl. ...
随机推荐
- 大前端系统学-了解html
标签: 使用尖括号包起来的就是标签,例如我们看到的 <html></html> 一对标签 <head> 开始标签 </head> 结束标签 < ...
- vuex环境配置及使用
vuex环境搭建 1.下载vuex,如果你是开发Vue2,请下载vuex3版本 npm i vuex@3 2.搭建vuex的环境配置 创建 src/state/index.js 文件 //导入Vu ...
- uni 结合vuex 编写动态全局配置变量 this.baseurl
在日常开发过程,相信大家有遇到过各种需求,而我,在这段事件便遇到了一个,需要通过用户界面配置动态接口,同时,因为是app小程序开发,所以接口中涉及到了http以及websocket两个类型的接口. 同 ...
- K8s架构|全面整理K8s的架构介绍
K8S架构与核心技术介绍 1. 架构图 1.1 整体结构图 1.2 组件间的协议 CNI: CNI是Container Network Interface的是一个标准的,通用的接口 ;用于连接容器管理 ...
- 【云原生 • Docker】mysql、tomcat、nginx、redis 环境部署
注意在应用部署之前,docker 服务必须是开启状态 systemctl start docker. 目录 一.MySQL 部署 二.Tomcat 部署 三.Nginx 部署 四.Redis 部署 一 ...
- Java工厂模式的最佳实践?
"Simplicity is prerequisite for reliability." - Edsger Dijkstra "简单是可靠的前提条件." -- ...
- React报错之Function components cannot have string refs
总览 当我们在一个函数组件中使用一个字符串作为ref时,会产生"Function components cannot have string refs"错误.为了解决该错误,使用u ...
- 大角度非迭代的空间坐标旋转C#实现
1. 绪论 在前面文章中提到空间直角坐标系相互转换,测绘坐标转换时,一般涉及到的情况是:两个直角坐标系的小角度转换.这个就是我们经常在测绘数据处理中,WGS-84坐标系.54北京坐标系.80西安坐标系 ...
- 命令指定IP端口号
tcping命令是针对tcp监控的,也可以看到ping值,即使源地址禁ping也可以通过tcping来监控服务器网络状态,除了简单的ping之外,tcping最大的一个特点就是可以指定端口. 将下载好 ...
- loadrunner11安装时提示缺少Microsoft Visual c++2005 sp1组件的解决办法
解决方法: 1.进入loadrunner-11安装程序\loadrunner-11\Additional Components\IDE Add-Ins\MS Visual Studio .NET文件夹 ...