CMPSC 311- Introduction to Systems Programming Module:...

24
CMPSC 311 - Introduction to Systems Programming CMPSC 311- Introduction to Systems Programming Module: Assignment #2 Cartridge HRAM Filesystem Professor Patrick McDaniel Fall 2016

Transcript of CMPSC 311- Introduction to Systems Programming Module:...

Page 1: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming

CMPSC 311- Introduction to Systems Programming

Module: Assignment #2���Cartridge HRAM Filesystem

Professor Patrick McDanielFall 2016

Page 2: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Assignment #2 •  Idea you are to maintain the correct file contents in an

HRAM system special purpose memory device.•  You will write code to communicate with a memory

controller that will store file-system data.•  3 things to know‣  How file I/O works

‣  How the cartridge memory controller works

‣  How to make the memory device stuff look like files

Page 3: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

The CART Device Driver

Simulated Application(PROVIDED)

Filesystem Driver(YOUR CODE)

CART memory controller Interface (PROVIDED)

(filesystem commands, e.g., open, read ..)

CART memory system (PROVIDED)

(CART commands, e.g., CART_INIT, CART_READBL ..)

Page 4: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Assignment Basics •  You are to write the file operation functions defined in

the cart_driver.c ‣  Translate then file operations into CART bus operations

‣  Maintain, in the memory device, the file contents•  You must insert data, read data, and maintain a file pointer and a

file allocation data.

•  Your code: open, read, write, close, seek …

‣  For this assignment (#2)•  The filesystem will only have one file..

•  It will not be bigger than 100k

•  Your code will be exercised by the simulator given to you (it will call your functions).

Page 5: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Maintaining file contents •  Open prepares an empty file for

reading (zero length)•  Write writes bytes into the file

•  Seek seeks to a particular position in the file (end if pos > length)

•  Read copies up to length number of bytes from the file

•  Close deletes the contents

len=0, pos=0

open(“file.txt”)

X X X X X

len=5, pos=5

write(“XXXXX”, 5)

X X X X X

len=5, pos=2

seek(2)

X X X X X

len=5, pos=5

read(4)

Page 6: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Driver code •  int32_t cart_poweron(void); - This function will initialize the CART interface

basic filesystem. To do this you will execute the init CART opcode and zero all of the memory locations. You will also need to setup your internal data structures that track the state of the filesystem.

•  int32_t cart_poweroff(void); - This function will show down the CART interface basic filesystem. To do this you will execute the shutdown CART opcode and close all of the files. You will also need to cleanup your internal data structures that track the state of the filesystem

•  int16_t cart_open(char *path); - This function will open a file (named path) in the filesystem. If the file does not exist, it should be created and set to zero length. If it does exist, it should be opened and its read/write position should be set to the first byte. Note that there are no subdirectories in the filesystem, just files (so you can just treat path a filename). The function should return a unique file handle used for subsequent operations or -1 if a failure happens.

•  int16_t cart_close(int16_t fd); - This function closes the file referenced by the file handle that was previously open. The function should fail (and return -1) if the file handle is bad or the file was not previously open.

Page 7: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Driver code (continued) •  int32_t cart_read(int16_t fd, void *buf, int32_t count); - This

function should read count bytes from the file referenced by the file handleat the current position. Note that if there are not enough bytes left in the file, the function should read to the end of the file and return the number of bytes read. If there are enough bytes to fulfill the read, the function should return count. The function should fail (and return -1) if the file handle is bad or the file was not previously open.

•  int32_t cart_write(int16_t fd, void *buf, int32_t count); - The function should write count bytes into the file referenced by the file handle. If the write goes beyond the end of the file the size should be increased. The function should always return the number of bytes written, e.g., count. The function should fail (and return -1) if the file handle is bad or the file was not previously open.

•  int32_t cart_seek(int16_t fd, uint32_t loc); - The function should set the current position into the file to loc, where 0 is the first byte in the file. The function should fail (and return -1) if the loc is beyond the end of the file, the file handle is bad or the file was not previously open.

Page 8: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

CART HRAM System

•  There are CART_MAX_CARTRIDGES cartridges in the system, each of which is numbered from 0 to CART_MAX_CARTRIDGES-1.

•  Each cartridge contains are CART_CARTRIDGE_SIZE frames, each of which is numbered from 0 to CART_MAX_CARTRIDGES-1.

•  A frame is memory block of CART_FRAME_SIZE bytes.

•  The system maintains a current cartridge register indicating which cartridge operations will be performed on.

Page 9: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

CART Opcode Execution

You communicate with memory controller through a set of packed registers that encode the opcode and arguments for the operation. The “data” associated with the opcode (where needed) is communicated through the fixed sized frame buffer.

CartXferRegister cart_io_bus(CartXferRegister regstate, void *frame)

Most significant

bitLeast

significant bit

KY1 (8) KY2 (8) CT1 (16) FM1 (16) unused

64 bits

RT1 (1)

Page 10: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

CART OpCodes

Page 11: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Summary •  To summarize, your code will receive commands from

the virtual application (simulator). It will do the following functions to implement the device driver:a.  Initialize the disk array by calling the init & bzero opcodes

b.  On writes needing "new" frames, find a location on the disk to put it, then execute the CART operations to get it to store the data

c.  On writes for old frames, figure out where you put it, then execute the CART operations to overwrite the data

d.  On reads, return the previously stored data in those frames

e.  Power off the controller when asked to

Page 12: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Testing your program •  The testing of the program is performed by using

the simulated workloads. The main function provided to you simple calls the function. To test the program, you execute the simulator using the -v option, as:

•  If you have implemented everything correctly, the log should display the following message:

./cart_sim–v workload/cmpsc311-f16-assign2-workload.txt

CART simulation: all tests successful!!!.

Page 13: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Getting started … •  Get the file from the web:

•  Change into your development directory and unpack the file:

•  Note: you may have to install libgcrypt-dev via apt-get.

wget http://www.cse.psu.edu/~mcdaniel/cmpsc311-f16/docs/assign2-starter.tgz

% cd ~/cmpsc311% cp assign2-starter.tgz cmpsc311% cd cmpsc311% tar xvfz assign2-starter.tgz% cd assign2% make

Page 14: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Hints •  Use the logMessage interface to log information about

how your program is running. •  Carefully read and understand the error messages that

are being written to the log.•  Review the simulator code to see how the interfaces in

the program should operate.

Page 15: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

First functions … •  The first functions you should write should create a

register structure and extract a register structure:

CartXferRegister create_cart_opcode(…?) {???

}

??? extract_cart_opcode(CartXferRegister resp, …) {???

}

Hint: use the bit operations to build packed registers ….

Page 16: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Help Notes Basic opcode operation // Call the bus command to initialze the interface if ((regstate = construct_cart_regstate(CART_OP_INITMS, 0, 0, 0, 0)) == -1) { logMessage(LOG_ERROR_LEVEL, "CART driver failed: fail on init."); return(-1); } oregstate = cart_io_bus(regstate, NULL); if (deconstruct_cart_regstate(oregstate, &ky1, &ky2, &rt1, &ct1, &fm1)) { logMessage(LOG_ERROR_LEVEL, "CART driver failed: failed: fail on init (decon)."); return(-1); } if (rt1) { logMessage(LOG_ERROR_LEVEL, "CART driver failed: failed: fail on init (return)."); return(-1); }

Page 17: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Example

FRM0 FRM1 FRM2 … Cartridge 0

FRM0 FRM1 FRM2 … Cartridge 1

FRM0 FRM1 FRM2 … Cartridge 2

FRM0 FRM1 FRM2 … …

WR 1 WR 2 WR 3 WR 4 WR 5File writes

Page 18: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Example

FRM0 FRM1 FRM2 … Cartridge 0

FRM0 FRM1 FRM2 … Cartridge 1

FRM0 FRM1 FRM2 … Cartridge 2

FRM0 FRM1 FRM2 … …

WR 1 WR 2 WR 3 WR 4 WR 5

Page 19: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Example

FRM0 FRM1 FRM2 … Cartridge 0

FRM0 FRM1 FRM2 … Cartridge 1

FRM0 FRM1 FRM2 … Cartridge 2

FRM0 FRM1 FRM2 … …

WR 1 WR 2 WR 3 WR 4 WR 5

Page 20: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Example

FRM0 FRM1 FRM2 … Cartridge 0

FRM0 FRM1 FRM2 … Cartridge 1

FRM0 FRM1 FRM2 … Cartridge 2

FRM0 FRM1 FRM2 … …

WR 1 WR 2 WR 3 WR 4 WR 5

Page 21: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Example

FRM0 FRM1 FRM2 … Cartridge 0

FRM0 FRM1 FRM2 … Cartridge 1

FRM0 FRM1 FRM2 … Cartridge 2

FRM0 FRM1 FRM2 … …

WR 1 WR 2 WR 3 WR 4 WR 5

Page 22: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Open pseudo-code Open (path) { 1) check if file with path already open (fail if already open) 2) pick unique file handle 3) save filename and file information locally 4) set file pointer to first byte 5) if file not exists set length to 0 return file handle

}

Page 23: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Read pseudo-code Read ( file handle, length, buffer ) { 1) check if file handle valid (is associated with open file) 2) check length to if it is valid 3) figure out what frame data for the read is 4) get the frame 5) copy the data from the frame to buffer 6) update the buffer length return the number of read bytes

}

Page 24: CMPSC 311- Introduction to Systems Programming Module: …pdm12/cmpsc311-f16/slides/cmpsc311-cart.pdf · 2016-12-09 · CMPSC 311 - Introduction to Systems Programming Page Driver

CMPSC 311 - Introduction to Systems Programming Page

Read pseudo-code Write ( file handle, length, buffer ) { 1) check if file handle valid (is associated with open file) 2) check length to if it is valid 3) figure out what frame data for the write is if not already existing frame, then allocate the frame 4) get the frame 5) copy the data from the frame to buffer 6) update the buffer length return the number of read bytes

}