From b3b60ea29adf9415c9c7b98ba331dacd92f231b7 Mon Sep 17 00:00:00 2001 From: Zach Koopmans Date: Mon, 26 Nov 2018 09:49:53 -0800 Subject: Implementation of preadv2 for Linux 4.4 support Implement RWF_HIPRI (4.6) silently passes the read call. Implement -1 offset calls readv. PiperOrigin-RevId: 222840324 Change-Id: If9ddc1e8d086e1a632bdf5e00bae08205f95b6b0 --- pkg/abi/linux/file.go | 7 ++++ pkg/sentry/syscalls/linux/linux64.go | 3 ++ pkg/sentry/syscalls/linux/sys_read.go | 62 +++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) (limited to 'pkg') diff --git a/pkg/abi/linux/file.go b/pkg/abi/linux/file.go index 72e5c6f83..8d48e1753 100644 --- a/pkg/abi/linux/file.go +++ b/pkg/abi/linux/file.go @@ -150,6 +150,13 @@ const ( PermissionsMask = 0777 ) +// Values for preadv2/pwritev2. +const ( + RWF_HIPRI = 0x0001 + RWF_DSYNC = 0X0002 + RWF_SYNC = 0x0004 +) + // Stat represents struct stat. type Stat struct { Dev uint64 diff --git a/pkg/sentry/syscalls/linux/linux64.go b/pkg/sentry/syscalls/linux/linux64.go index 9912ab2b5..2aab948da 100644 --- a/pkg/sentry/syscalls/linux/linux64.go +++ b/pkg/sentry/syscalls/linux/linux64.go @@ -373,7 +373,10 @@ var AMD64 = &kernel.SyscallTable{ // 322: Execveat, TODO // 323: Userfaultfd, TODO // 324: Membarrier, TODO + // Syscalls after 325 are backports from 4.6. 325: syscalls.Error(nil), // Mlock2, TODO + 327: Preadv2, + // 328: Pwritev2, // Pwritev2, TODO }, Emulate: map[usermem.Addr]uintptr{ diff --git a/pkg/sentry/syscalls/linux/sys_read.go b/pkg/sentry/syscalls/linux/sys_read.go index b2e5a5449..cbb9eb9f8 100644 --- a/pkg/sentry/syscalls/linux/sys_read.go +++ b/pkg/sentry/syscalls/linux/sys_read.go @@ -187,6 +187,68 @@ func Preadv(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.Syscal return uintptr(n), nil, handleIOError(t, n != 0, err, kernel.ERESTARTSYS, "preadv", file) } +// Preadv2 implements linux syscall preadv2(2). +func Preadv2(t *kernel.Task, args arch.SyscallArguments) (uintptr, *kernel.SyscallControl, error) { + fd := kdefs.FD(args[0].Int()) + addr := args[1].Pointer() + iovcnt := int(args[2].Int()) + offset := args[3].Int64() + flags := int(args[4].Int()) + + validFlags := linux.RWF_HIPRI + + file := t.FDMap().GetFile(fd) + if file == nil { + return 0, nil, syserror.EBADF + } + defer file.DecRef() + + // Check that the offset is legitimate. + if offset < -1 { + return 0, nil, syserror.EINVAL + } + + // Is reading at an offset supported? + if offset > -1 && !file.Flags().Pread { + return 0, nil, syserror.ESPIPE + } + + // Check that the file is readable. + if !file.Flags().Read { + return 0, nil, syserror.EBADF + } + + // Check flags field. + if flags != 0 { + if flags&^validFlags != 0 { + return 0, nil, syserror.EINVAL + } + // RWF_HIPRI must be called on a file with O_DIRECT flag set. + if flags&linux.RWF_HIPRI != 0 && !file.Flags().Direct { + return 0, nil, syserror.EINVAL + } + } + + // Read the iovecs that specify the destination of the read. + dst, err := t.IovecsIOSequence(addr, iovcnt, usermem.IOOpts{ + AddressSpaceActive: true, + }) + if err != nil { + return 0, nil, err + } + + // If preadv2 is called with an offset of -1, readv is called. + if offset == -1 { + n, err := readv(t, file, dst) + t.IOUsage().AccountReadSyscall(n) + return uintptr(n), nil, handleIOError(t, n != 0, err, kernel.ERESTARTSYS, "preadv2", file) + } + + n, err := preadv(t, file, dst, offset) + t.IOUsage().AccountReadSyscall(n) + return uintptr(n), nil, handleIOError(t, n != 0, err, kernel.ERESTARTSYS, "preadv2", file) +} + func readv(t *kernel.Task, f *fs.File, dst usermem.IOSequence) (int64, error) { n, err := f.Readv(t, dst) if err != syserror.ErrWouldBlock || f.Flags().NonBlocking { -- cgit v1.2.3