資源簡介
斯坦福大學課程設計pintos-project2滿分例程,ubuntu16.04qemu&bochs;均滿分(修改userprog/Make.vars 默認qemu)。注釋不多,部分內容參考github。代碼僅供參考,請勿直接搬運。測試直接在userprog目錄下make check。
代碼片段和文件信息
#include?“devices/block.h“
#include?
#include?
#include?
#include?“devices/ide.h“
#include?“threads/malloc.h“
/*?A?block?device.?*/
struct?block
??{
????struct?list_elem?list_elem;?????????/*?Element?in?all_blocks.?*/
????char?name[16];??????????????????????/*?Block?device?name.?*/
????enum?block_type?type;????????????????/*?Type?of?block?device.?*/
????block_sector_t?size;?????????????????/*?Size?in?sectors.?*/
????const?struct?block_operations?*ops;??/*?Driver?operations.?*/
????void?*aux;??????????????????????????/*?Extra?data?owned?by?driver.?*/
????unsigned?long?long?read_cnt;????????/*?Number?of?sectors?read.?*/
????unsigned?long?long?write_cnt;???????/*?Number?of?sectors?written.?*/
??};
/*?List?of?all?block?devices.?*/
static?struct?list?all_blocks?=?LIST_INITIALIZER?(all_blocks);
/*?The?block?block?assigned?to?each?Pintos?role.?*/
static?struct?block?*block_by_role[BLOCK_ROLE_CNT];
static?struct?block?*list_elem_to_block?(struct?list_elem?*);
/*?Returns?a?human-readable?name?for?the?given?block?device
???TYPE.?*/
const?char?*
block_type_name?(enum?block_type?type)
{
??static?const?char?*block_type_names[BLOCK_CNT]?=
????{
??????“kernel“
??????“filesys“
??????“scratch“
??????“swap“
??????“raw“
??????“foreign“
????};
??ASSERT?(type???return?block_type_names[type];
}
/*?Returns?the?block?device?fulfilling?the?given?ROLE?or?a?null
???pointer?if?no?block?device?has?been?assigned?that?role.?*/
struct?block?*
block_get_role?(enum?block_type?role)
{
??ASSERT?(role???return?block_by_role[role];
}
/*?Assigns?BLOCK?the?given?ROLE.?*/
void
block_set_role?(enum?block_type?role?struct?block?*block)
{
??ASSERT?(role???block_by_role[role]?=?block;
}
/*?Returns?the?first?block?device?in?kernel?probe?order?or?a
???null?pointer?if?no?block?devices?are?registered.?*/
struct?block?*
block_first?(void)
{
??return?list_elem_to_block?(list_begin?(&all_blocks));
}
/*?Returns?the?block?device?following?BLOCK?in?kernel?probe
???order?or?a?null?pointer?if?BLOCK?is?the?last?block?device.?*/
struct?block?*
block_next?(struct?block?*block)
{
??return?list_elem_to_block?(list_next?(&block->list_elem));
}
/*?Returns?the?block?device?with?the?given?NAME?or?a?null
???pointer?if?no?block?device?has?that?name.?*/
struct?block?*
block_get_by_name?(const?char?*name)
{
??struct?list_elem?*e;
??for?(e?=?list_begin?(&all_blocks);?e?!=?list_end?(&all_blocks);
???????e?=?list_next?(e))
????{
??????struct?block?*block?=?list_entry?(e?struct?block?list_elem);
??????if?(!strcmp?(name?block->name))
????????return?block;
????}
??return?NULL;
}
/*?Verifies?that?SECTOR?is?a?valid?offset?within?BLOCK.
???Panics?if?not.?*/
static?void
check_sector?(struct?block?*block?block_sector_t?sector)
{
??if?(sector?>=?block->size)
????{
??????/*?We?do?not?use?ASSERT?because?we?want?to?panic?here
?????????regardless?of?whether?NDEBUG?is?defined.?*/
??????PANIC?(“Access?past?end?of?device?%s?
評論
共有 條評論