12 - Input and Output
12 - Input and Output
1
Readings and Exercises
• ARMv8 Instruction Set Overview: Section 5.9.1.1
2
Objectives
At the end of this section, you will be able to:
1. Perform I/O using system calls
2. Understand the UNIX I/O file-interface with
devices
3. Open, read, write, and close files
4. Use the standard I/O as files
3
Introduction
• A computer may do input and output in a variety
of ways:
▪ Interrupt-driven I/O
▪ Memory-mapped I/O
▪ Port I/O
▪ System I/O
4
Introduction
• Since our ARM servers are running a Linux OS,
only system I/O is available
▪ User-level programs communicate with external
devices through the OS
• Prevents malicious or accidental interactions that may
damage the device
Application
OS
Hardware
5
System I/O
• A program running in exception level 0 (EL0)
does I/O by making a system call
▪ It generates an exception using the svc instruction
▪ Like a subroutine call
• Control is transferred to a predefined system function
▪ The address for this is stored in a table in the OS
• The system code executes at exception level 1 (EL1)
▪ Is able to interact directly with I/O devices
• Once finished, control returns to the calling code
▪ Changes back to EL0
6
UNIX I/O
• In UNIX (Linux), all peripheral devices are
represented as files
• Provides a uniform interface for I/O
• File I/O involves interacting with secondary
memory (usually a disk)
• Standard I/O involves the keyboard and screen
devices
7
UNIX System Calls
• The type of system call is determined by the
number put into x8 before invoking svc
▪ I/O Service requests:
x8 Service Request
56 openat
57 close
63 read
64 write
8
UNIX System Calls
• Example to close a file:
mov x8, 57 // close file code
svc 0 // supervisor call; 0 ignored by CPU
9
UNIX System Call Pattern
• Open the file
▪ connect to the device, get its file descriptor
• Read from or write to the file
▪ do device I/O, transferring bytes
• Close the file
▪ disconnect from the device
10
Opening a File
• Equivalent C function:
int fd = openat(int dirfd, const char *pathname, int flags,
mode_t mode);
11
Opening a File
▪ flags: combination of constants (using |) indicating
what will be done to the file’s data
• Choose only one of:
O_RDONLY 00 Read-only access
O_WRONLY 01 Write-only access
O_RDWR 02 Read/write access
• Optional flags:
O_CREAT 0100 Create file if it doesn’t exist
O_EXCL 0200 Fail if file exists (with O_CREAT)
O_TRUNC 01000 Truncate an existing file
O_APPEND 02000 Append access
• Note: flags are defined in /usr/include/bits/fcntl-linux.h
12
Opening a File
▪ mode: optional argument that specifies UNIX file
permissions
• Required only when creating a new file (using O_CREAT)
• Specified in octal
• Eg: 0700 specifies read/write/execute permission for file
owner, no permissions for group and others
▪ fd: the returned file descriptor
• Is -1 on error
13
Opening a File: Example
▪ Eg: opening an existing file called myfile.bin
pn: .string “myfile.bin”
. . .
mov w0, -100 // 1st arg (use cwd)
adrp x1, pn // 2nd arg (pathname)
add x1, x1, :lo12:pn
mov w2, 0 // 3rd arg (read only)
mov w3, 0 // 4th arg (not used)
mov x8, 56 // openat I/O request
svc 0 // call system function
cmp w0, 0 // error check
b.ge open_ok
. . . // error handling code
open_ok:
. . . // fd is in w0
14
Reading a File
• Equivalent C function:
long n_read = read(int fd, void *buf, unsigned
long n);
16
Writing a File
• Equivalent C function:
long n_written = write(int fd, void *buf, unsigned long n);
19
Standard I/O
• Input and output streams predefined by the OS
▪ Always available
• Are not opened or closed
• Standard input (stdin)
▪ Represents input coming from the keyboard
▪ File descriptor: 0
20
Standard I/O (cont’d)
• Standard output (stdout)
▪ Represents output to the monitor screen
▪ File descriptor: 1
• Standard error (stderr)
▪ Also represents output to the monitor screen
• Normally used to output error messages
▪ File descriptor: 2
21