Linux-0.0.1内核阅读连载笔记-2013.08.23

简介: __________________________________________________________ ../kernel/Vsprintf.c __________________________________________________________ #include <linux/config.h> #include <linu
__________________________________________________________

../kernel/Vsprintf.c

__________________________________________________________

#include <linux/config.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/hdreg.h>
#include <asm/system.h>
#include <asm/io.h>
#include <asm/segment.h>

/*
 * This code handles all hd-interrupts, and read/write requests to
 * the hard-disk. It is relatively straigthforward (not obvious maybe,
 * but interrupts never are), while still being efficient, and never
 * disabling interrupts (except to overcome possible race-condition).
 * The elevator block-seek algorithm doesn't need to disable interrupts
 * due to clever programming.
 */

/* Max read/write errors/sector */
#define MAX_ERRORS 5
#define MAX_HD  2
#define NR_REQUEST 32

/*
 *  This struct defines the HD's and their types.
 *  Currently defined for CP3044's, ie a modified
 *  type 17.
 */
static struct hd_i_struct{
 int head,sect,cyl,wpcom,lzone,ctl;
 } hd_info[]= { HD_TYPE };

#define NR_HD ((sizeof (hd_info))/(sizeof (struct hd_i_struct)))

static struct hd_struct {
 long start_sect;
 long nr_sects;
} hd[5*MAX_HD]={{0,0},};   //一个硬盘可分5个区,共有2个硬盘

static struct hd_request {
 int hd;  /* -1 if no request */
 int nsector;
 int sector;
 int head;
 int cyl;
 int cmd;
 int errors;
 struct buffer_head * bh;
 struct hd_request * next;
} request[NR_REQUEST];

#define IN_ORDER(s1,s2) \
((s1)->hd<(s2)->hd || (s1)->hd==(s2)->hd && \
((s1)->cyl<(s2)->cyl || (s1)->cyl==(s2)->cyl && \
((s1)->head<(s2)->head || (s1)->head==(s2)->head && \
((s1)->sector<(s2)->sector))))

static struct hd_request * this_request = NULL;

static int sorting=0;

static void do_request(void);
static void reset_controller(void);
static void rw_abs_hd(int rw,unsigned int nr,unsigned int sec,unsigned int head,
 unsigned int cyl,struct buffer_head * bh);
void hd_init(void);

#define port_read(port,buf,nr) \
//下面的内嵌汇编的意思是:
// movl port %edx
// movl buf %edi
// movl nr %ecx
// cld
// rep insw  //从edx指定的port传送字到es:edi指定的buf中
__asm__("cld;rep;insw"::"d" (port),"D" (buf),"c" (nr):"cx","di")

#define port_write(port,buf,nr) \
__asm__("cld;rep;outsw"::"d" (port),"S" (buf),"c" (nr):"cx","si") //其中outsw是从ds:si指定的buf中传送字到edx指定的port

extern void hd_interrupt(void);   //在system_call.S中定义

static struct task_struct * wait_for_request=NULL;

static inline void lock_buffer(struct buffer_head * bh)
{
 if (bh->b_lock)    //此缓冲区是否上锁
  printk("hd.c: buffer multiply locked\n");
 bh->b_lock=1;
}

static inline void unlock_buffer(struct buffer_head * bh)
{
 if (!bh->b_lock)
  printk("hd.c: free buffer being unlocked\n");
 bh->b_lock=0;
 wake_up(&bh->b_wait);
}

static inline void wait_on_buffer(struct buffer_head * bh)
{
 cli();
 while (bh->b_lock)
  sleep_on(&bh->b_wait);  //进入等待对列
 sti();
}

void rw_hd(int rw, struct buffer_head * bh) //缓冲区和磁盘之间交换数据
{
 unsigned int block,dev;
 unsigned int sec,head,cyl;

 block = bh->b_blocknr << 1;   //默认1块=1024字节,二个扇区
 dev = MINOR(bh->b_dev);
 if (dev >= 5*NR_HD || block+2 > hd[dev].nr_sects) //是否超过了分区,此分区是否满了
  return;
 block += hd[dev].start_sect;  //此分区的开始扇区+block,即在硬盘中定开始的位置
 dev /= 5;     //看看是哪个硬盘
 __asm__("divl %4":"=a" (block),"=d" (sec):"0" (block),"1" (0), 
  "r" (hd_info[dev].sect));     //确定扇区号
 __asm__("divl %4":"=a" (cyl),"=d" (head):"0" (block),"1" (0),
  "r" (hd_info[dev].head));     //确定光头号
 rw_abs_hd(rw,dev,sec+1,head,cyl,bh); //实际写盘函数
}

/* This may be used only once, enforced by 'static int callable' */
int sys_setup(void)
{
 static int callable = 1;
 int i,drive;
 struct partition *p;

 if (!callable)
  return -1;
 callable = 0;
 for (drive=0 ; drive<NR_HD ; drive++) {
  rw_abs_hd(READ,drive,1,0,0,(struct buffer_head *) start_buffer); //读取引导扇区512字节到start_buffer中
  if (!start_buffer->b_uptodate) {
   printk("Unable to read partition table of drive %d\n\r",
    drive);
   panic("");
  }
  if (start_buffer->b_data[510] != 0x55 || (unsigned char)
      start_buffer->b_data[511] != 0xAA) {    //分析引导扇区是否合法,合法应为55AA
   printk("Bad partition table on drive %d\n\r",drive);
   panic("");
  }
  p = 0x1BE + (void *)start_buffer->b_data;  //指针指向分区信息的开始位置
  for (i=1;i<5;i++,p++) {    //保存分区信息
   hd[i+5*drive].start_sect = p->start_sect;
   hd[i+5*drive].nr_sects = p->nr_sects;
  }
 }
 printk("Partition table%s ok.\n\r",(NR_HD>1)?"s":"");
 mount_root();    //在super.c中定义
 return (0);
}

/*
 * This is the pointer to a routine to be executed at every hd-interrupt.
 * Interesting way of doing things, but should be rather practical.
 */
void (*do_hd)(void) = NULL;

static int controller_ready(void)
{
 int retries=1000;

 while (--retries && (inb(HD_STATUS)&0xc0)!=0x40); //0x40是第六个位置1为驱动器准备好,0x1f7端口是读取硬盘状态
 return (retries);
}

static int win_result(void)
{
 int i=inb(HD_STATUS); //读取硬盘状态

 if ((i & (BUSY_STAT | READY_STAT | WRERR_STAT | SEEK_STAT | ERR_STAT))
  == (READY_STAT | SEEK_STAT))
  return(0); /* ok */
 if (i&1) i=inb(HD_ERROR); //0x1f1是硬盘错误寄存器,检查上一个命令是否出错
 return (1);
}

static void hd_out(unsigned int drive,unsigned int nsect,unsigned int sect,
  unsigned int head,unsigned int cyl,unsigned int cmd,
  void (*intr_addr)(void))
{
 register int port asm("dx");

 if (drive>1 || head>15) //只支持1个硬盘和14个头
  panic("Trying to write bad sector");
 if (!controller_ready())
  panic("HD controller not ready");
 do_hd = intr_addr;  //硬盘写或读操作指针
 outb(_CTL,HD_CMD);  //0x3f6是硬盘控制寄存器,置0为控制器为普通操作
 port=HD_DATA;   //0x1f0硬盘数据寄存器
 outb_p(_WPCOM,++port); //0x1f1,写预补偿,在从磁柱300X4处开始写
 outb_p(nsect,++port); //0x1f2是硬盘扇区记数寄存器,将要传送的扇区数放入该寄存器
 outb_p(sect,++port);  //0x1f3是硬盘扇区号寄存器,记录当前的扇区号
 outb_p(cyl,++port);  //0x1f4硬盘磁柱低寄存器,保存起始磁柱号的低8字节
 outb_p(cyl>>8,++port); //0x1f5硬盘磁柱高寄存器
 outb_p(0xA0|(drive<<4)|head,++port); //0x1f6硬盘驱动器和磁头寄存器,这里选择主盘(高4位为1010),低4位为磁头号
 outb(cmd,++port);  //0x1f7硬盘命令输出寄存器,主要向硬盘发出命令
}

static int drive_busy(void)
{
 unsigned int i;

 for (i = 0; i < 100000; i++) //循环,直到驱动器为READY_STAT或超时
  if (READY_STAT == (inb(HD_STATUS) & (BUSY_STAT | READY_STAT)))
   break;
 i = inb(HD_STATUS);   //再读硬盘状态
 i &= BUSY_STAT | READY_STAT | SEEK_STAT;
 if (i == READY_STAT | SEEK_STAT) //如果处于读状态或寻找状态,OK
  return(0);
 printk("HD controller times out\n\r");
 return(1);
}

static void reset_controller(void)
{
 int i;

 outb(4,HD_CMD);  //0x3f6是硬盘适配器控制寄存器,第二位置1为重启控制器
 for(i = 0; i < 1000; i++) nop(); //等待一会儿
 outb(0,HD_CMD);  //使驱动器处于普通状态
 for(i = 0; i < 10000 && drive_busy(); i++) /* nothing */;
 if (drive_busy())
  printk("HD-controller still busy\n\r");
 if((i = inb(ERR_STAT)) != 1)
  printk("HD-controller reset failed: %02x\n\r",i);
}

static void reset_hd(int nr)
{
 reset_controller();
 hd_out(nr,_SECT,_SECT,_HEAD-1,_CYL,WIN_SPECIFY,&do_request); //do_request是重新处理请求队列
}

void unexpected_hd_interrupt(void)
{
 panic("Unexpected HD interrupt\n\r");
}

static void bad_rw_intr(void)
{
 int i = this_request->hd;

 if (this_request->errors++ >= MAX_ERRORS) { //如果错误超过有效的最大错误数,放弃这次请求
  this_request->bh->b_uptodate = 0;
  unlock_buffer(this_request->bh);
  wake_up(&wait_for_request);
  this_request->hd = -1;
  this_request=this_request->next;
 }
 reset_hd(i);
}

static void read_intr(void)
{
 if (win_result()) {
  bad_rw_intr();
  return;
 }
 port_read(HD_DATA,this_request->bh->b_data+
  512*(this_request->nsector&1),256); //是从b_data指定位置读512字节
 this_request->errors = 0;
 if (--this_request->nsector)  //如果还有要传的数据,直接返回,不从请求队列中删除该请求
  return;
//以下6行代码是指数据传送完毕,就从请求队列中删除该请求
 this_request->bh->b_uptodate = 1;
 this_request->bh->b_dirt = 0;
 wake_up(&wait_for_request);
 unlock_buffer(this_request->bh);
 this_request->hd = -1;
 this_request=this_request->next;
 do_request();    //处理其他请求
}

static void write_intr(void)
{
 if (win_result()) {
  bad_rw_intr();  //如果有错误,重启驱动器,再继续处理请求
  return;
 }
 if (--this_request->nsector) {
  port_write(HD_DATA,this_request->bh->b_data+512,256); //向硬盘数据寄存器写数据
  return;
 }
 this_request->bh->b_uptodate = 1;
 this_request->bh->b_dirt = 0;
 wake_up(&wait_for_request);
 unlock_buffer(this_request->bh);
 this_request->hd = -1;
 this_request=this_request->next;
 do_request();    //处理下一个请求
}

static void do_request(void)
{
 int i,r;

 if (sorting)
  return;
 if (!this_request) {
  do_hd=NULL;
  return;
 }
 if (this_request->cmd == WIN_WRITE) {
  hd_out(this_request->hd,this_request->nsector,this_request->
   sector,this_request->head,this_request->cyl,
   this_request->cmd,&write_intr);
  for(i=0 ; i<3000 && !(r=inb_p(HD_STATUS)&DRQ_STAT) ; i++) //循环,直到控制器指示准备好
   /* nothing */ ;
  if (!r) {     //如果超时驱动器仍未准备好,重设该驱动器
   reset_hd(this_request->hd);
   return;
  }
  port_write(HD_DATA,this_request->bh->b_data+
   512*(this_request->nsector&1),256);  //传送数据
 } else if (this_request->cmd == WIN_READ) {
  hd_out(this_request->hd,this_request->nsector,this_request->
   sector,this_request->head,this_request->cyl,
   this_request->cmd,&read_intr);
 } else
  panic("unknown hd-command");
}

/*
 * add-request adds a request to the linked list.
 * It sets the 'sorting'-variable when doing something
 * that interrupts shouldn't touch.
 */
static void add_request(struct hd_request * req)
{
 struct hd_request * tmp;

 if (req->nsector != 2)
  panic("nsector!=2 not implemented");
/*
 * Not to mess up the linked lists, we never touch the two first
 * entries (not this_request, as it is used by current interrups,
 * and not this_request->next, as it can be assigned to this_request).
 * This is not too high a price to pay for the ability of not
 * disabling interrupts.
 */
 sorting=1;
 if (!(tmp=this_request))
  this_request=req;
 else {
  if (!(tmp->next))
   tmp->next=req;
  else {
   tmp=tmp->next;
   for ( ; tmp->next ; tmp=tmp->next)
    if ((IN_ORDER(tmp,req) ||
        !IN_ORDER(tmp,tmp->next)) &&
        IN_ORDER(req,tmp->next))
     break;
   req->next=tmp->next;
   tmp->next=req;
  }
 }
 sorting=0;
/*
 * NOTE! As a result of sorting, the interrupts may have died down,
 * as they aren't redone due to locking with sorting=1. They might
 * also never have started, if this is the first request in the queue,
 * so we restart them if necessary.
 */
 if (!do_hd)
  do_request();
}

void rw_abs_hd(int rw,unsigned int nr,unsigned int sec,unsigned int head,
 unsigned int cyl,struct buffer_head * bh)     //根据硬盘参数做磁盘读写
{
 struct hd_request * req;

 if (rw!=READ && rw!=WRITE)
  panic("Bad hd command, must be R/W");
 lock_buffer(bh);
repeat:
 for (req=0+request ; req<NR_REQUEST+request ; req++)
  if (req->hd<0)     //hd=-1表示没有请求
   break;
 if (req==NR_REQUEST+request) {    //不能超过最大的请求数
  sleep_on(&wait_for_request);
  goto repeat;
 }
 req->hd=nr;
 req->nsector=2;      //==1024字节
 req->sector=sec;
 req->head=head;
 req->cyl=cyl;
 req->cmd = ((rw==READ)?WIN_READ:WIN_WRITE);
 req->bh=bh;
 req->errors=0;
 req->next=NULL;
 add_request(req);
 wait_on_buffer(bh);      //进入等待
}

void hd_init(void)
{
 int i;

 for (i=0 ; i<NR_REQUEST ; i++) {    //初始化请求队列
  request[i].hd = -1;
  request[i].next = NULL;
 }
 for (i=0 ; i<NR_HD ; i++) {     //初始化硬盘分区
  hd[i*5].start_sect = 0;
  hd[i*5].nr_sects = hd_info[i].head*
    hd_info[i].sect*hd_info[i].cyl;
 }
 set_trap_gate(0x2E,&hd_interrupt);    //登记硬盘中断处理程序,中断号为0x2E
 outb_p(inb_p(0x21)&0xfb,0x21);    //开放IRQ2(中断控制器1)
 outb(inb_p(0xA1)&0xbf,0xA1);    //开放IRQ14,硬盘控制器
}

 

目录
相关文章
|
9天前
|
Linux C语言
Linux内核队列queue.h
Linux内核队列queue.h
|
28天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 生成并更新内核模块的依赖 depmod命令 使用指南
【Shell 命令集合 系统设置 】Linux 生成并更新内核模块的依赖 depmod命令 使用指南
30 0
|
28天前
|
Shell Linux C语言
【Shell 命令集合 系统设置 】⭐Linux 卸载已加载的内核模块rmmod命令 使用指南
【Shell 命令集合 系统设置 】⭐Linux 卸载已加载的内核模块rmmod命令 使用指南
29 1
|
2天前
|
算法 Linux 调度
深入理解Linux内核的进程调度机制
【4月更文挑战第17天】在多任务操作系统中,进程调度是核心功能之一,它决定了处理机资源的分配。本文旨在剖析Linux操作系统内核的进程调度机制,详细讨论其调度策略、调度算法及实现原理,并探讨了其对系统性能的影响。通过分析CFS(完全公平调度器)和实时调度策略,揭示了Linux如何在保证响应速度与公平性之间取得平衡。文章还将评估最新的调度技术趋势,如容器化和云计算环境下的调度优化。
|
7天前
|
算法 Linux 调度
深度解析:Linux内核的进程调度机制
【4月更文挑战第12天】 在多任务操作系统如Linux中,进程调度机制是系统的核心组成部分之一,它决定了处理器资源如何分配给多个竞争的进程。本文深入探讨了Linux内核中的进程调度策略和相关算法,包括其设计哲学、实现原理及对系统性能的影响。通过分析进程调度器的工作原理,我们能够理解操作系统如何平衡效率、公平性和响应性,进而优化系统表现和用户体验。
18 3
|
14天前
|
Linux API C语言
FFmpeg开发笔记(一)搭建Linux系统的开发环境
本文指导初学者如何在Linux上搭建FFmpeg开发环境。首先,由于FFmpeg依赖第三方库,可以免去编译源码的复杂过程,直接安装预编译的FFmpeg动态库。推荐网站<https://github.com/BtbN/FFmpeg-Builds/releases>提供适用于不同系统的FFmpeg包。但在安装前,需确保系统有不低于2.22版本的glibc库。详细步骤包括下载glibc-2.23源码,配置、编译和安装。接着,下载Linux版FFmpeg安装包,解压至/usr/local/ffmpeg,并设置环境变量。最后编写和编译简单的C或C++测试程序验证FFmpeg环境是否正确配置。
35 8
FFmpeg开发笔记(一)搭建Linux系统的开发环境
|
14天前
|
负载均衡 算法 Linux
深度解析:Linux内核调度器的演变与优化策略
【4月更文挑战第5天】 在本文中,我们将深入探讨Linux操作系统的核心组成部分——内核调度器。文章将首先回顾Linux内核调度器的发展历程,从早期的简单轮转调度(Round Robin)到现代的完全公平调度器(Completely Fair Scheduler, CFS)。接着,分析当前CFS面临的挑战以及社区提出的各种优化方案,最后提出未来可能的发展趋势和研究方向。通过本文,读者将对Linux调度器的原理、实现及其优化有一个全面的认识。
|
15天前
|
Ubuntu Linux
Linux查看内核版本
在Linux系统中查看内核版本有多种方法:1) 使用`uname -r`命令直接显示版本号;2) 通过`cat /proc/version`查看内核详细信息;3) 利用`dmesg | grep Linux`显示内核版本行;4) 如果支持,使用`lsb_release -a`查看发行版及内核版本。
36 6
|
17天前
|
Linux 内存技术
Linux内核读取spi-nor flash sn
Linux内核读取spi-nor flash sn
14 1
|
24天前
|
存储 网络协议 Linux
【Linux 解惑 】谈谈你对linux内核的理解
【Linux 解惑 】谈谈你对linux内核的理解
23 0