0% found this document useful (0 votes)
16 views

12 - Input and Output

Uploaded by

ranbir singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

12 - Input and Output

Uploaded by

ranbir singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

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

Note: Full list in /usr/include/asm-generic/unistd.h

8
UNIX System Calls
• Example to close a file:
mov x8, 57 // close file code
svc 0 // supervisor call; 0 ignored by CPU

• Arguments to system calls are put into registers x0 -


x5
• Any return value is in x0

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);

▪ dirfd: directory file descriptor


• Used only if pathname is a relative path
• Can be set to AT_FDCWD (the value -100) to indicate that
the pathname is relative to the program’s current working
directory
▪ pathname: absolute or relative pathname of a file

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);

▪ fd: file descriptor


• Previously set by openat(), or 0 for stdin
▪ buf: buffer where the bytes read will be stored
• Usually a local variable
▪ n: number of bytes to read
• Must be ≤ buffer size
▪ n_read: number of bytes actually read
• -1 is returned on error 15
Reading a File: Example
▪ Eg: assembly code to read in 8 bytes
buf_size = 8
alloc = -(16 + buf_size) & -16
dealloc = -alloc
buf_s = 16
. . .
main: stp x29, x30, [sp, alloc]!
mov x29, sp
. . . // open file, and
. . . // put fd in w19
mov w0, w19 // 1st arg (fd)
add x1, x29, buf_s // 2nd arg (ptr to buf)
mov x2, 8 // 3rd arg (n)
mov x8, 63 // read I/O request
svc 0 // call sys function
. . . // n_read is in x0

16
Writing a File
• Equivalent C function:
long n_written = write(int fd, void *buf, unsigned long n);

▪ fd: file descriptor


• Previously set by openat(), or 1 for stdout
▪ buf: buffer where the bytes to write are stored
• Usually a local variable
▪ n: number of bytes to write
• Must be ≤ buffer size
▪ n_written: number of bytes actually written
• -1 is returned on error 17
Writing a File: Example
▪ Eg: assembly code to write out 8 bytes
buf_size = 8
alloc = -(16 + buf_size) & -16
dealloc = -alloc
buf_s = 16
. . .
main: stp x29, x30, [sp, alloc]!
mov x29, sp
. . . // open file, and
. . . // put fd in w19
. . . // fill buf with data
mov w0, w19 // 1st arg (fd)
add x1, x29, buf_s // 2nd arg (ptr to buf)
mov x2, 8 // 3rd arg (n)
mov x8, 64 // write I/O request
svc 0 // call sys function
. . . // n_written is in x0
18
Closing a File
• Equivalent C function:
int status = close(int fd);

▪ fd: file descriptor


▪ status: 0 if successful, -1 otherwise
• Assembly code:
. . . // assume fd is in w19
mov w0, w19 // 1st arg (fd)
mov x8, 57 // close I/O request
svc 0 // call sys function
. . . // status is in w0

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy