newlib/winsup/cygwin/fhandler_mem.cc

442 lines
9.4 KiB
C++
Raw Normal View History

/* fhandler_mem.cc. See fhandler.h for a description of the fhandler classes.
Copyright 2000, 2001, 2002 Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <unistd.h>
#include <sys/mman.h>
#include <ntdef.h>
#include "cygerrno.h"
#include "security.h"
#include "fhandler.h"
#include "ntdll.h"
/**********************************************************************/
/* fhandler_dev_mem */
fhandler_dev_mem::fhandler_dev_mem (int nunit)
: fhandler_base (FH_MEM), unit (nunit)
{
/* Reading physical memory only supported on NT/W2K. */
if (!wincap.has_physical_mem_access ())
{
mem_size = 0;
return;
}
if (unit == 1) /* /dev/mem */
{
NTSTATUS ret;
SYSTEM_BASIC_INFORMATION sbi;
if ((ret = NtQuerySystemInformation (SystemBasicInformation, (PVOID) &sbi,
sizeof sbi, NULL)) != STATUS_SUCCESS)
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
debug_printf("NtQuerySystemInformation: ret = %d, Dos(ret) = %d",
ret, RtlNtStatusToDosError (ret));
mem_size = 0;
}
else
mem_size = sbi.PhysicalPageSize * sbi.NumberOfPhysicalPages;
debug_printf ("MemSize: %d MB", mem_size >> 20);
}
else if (unit == 2) /* /dev/kmem - Not yet supported */
{
mem_size = 0;
debug_printf ("KMemSize: %d MB", mem_size >> 20);
}
else if (unit == 4) /* /dev/port == First 64K of /dev/mem */
{
mem_size = 65536;
debug_printf ("PortSize: 64 KB");
}
else
{
mem_size = 0;
debug_printf ("Illegal minor number!!!");
}
}
fhandler_dev_mem::~fhandler_dev_mem (void)
{
}
int
* dcrt0.cc (dll_crt0_1): Don't close hexec_proc if it is NULL. * fork.cc (vfork): Add debugging statements. * path.cc (get_device_number): Make static. Rewrite to inspect both unix and windows paths. (get_raw_device_number): Just check for parts of raw device that we care about. (get_devn): New function, pulled from get_device_number. (win32_device_name): Accomodate arg changes to get_device_number. (mount_info::get_device_number): Call get_device_number on translated Windows path. * spawn.cc (spawn_guts): Don't treat P_VFORK differently from P_NOWAIT. Add handle to child's shared region to child so that it will be preserved if the parent goes away. * fhandler.h: Throughout, simplify to one open method for all fhandler classes, requiring a path_conv first element. * fhandler.cc (fhandler_base::open): Remove obsolete method. Generalize to require path_conv * as first argument. (fhandler_disk_file::open): Remove obsolete method. (fhandler_disk_file::open): Use path_conv pointer rather than reference. * fhandler_clipboard.cc (fhandler_dev_clipboard::dup): Use new open method. (fhandler_dev_clipboard::open): Accomodate new argument for open methods. * fhandler_console.cc (fhandler_console::open): Ditto. (fhandler_console::dup): Use new open method. (fhandler_console::fixup_after_fork): Ditto. (fhandler_console::fixup_after_exec): Ditto. * fhandler_dsp.cc (fhandler_dev_dsp::open): Accomodate new argument for open methods. * fhandler_floppy.cc (fhandler_dev_floppy::open): Ditto. * fhandler_mem.cc (fhandler_dev_mem::open): Ditto. * fhandler_random (fhandler_dev_random::open): Ditto. * fhandler_raw.cc (fhandler_dev_raw::open): Ditto. * fhandler_serial.cc (fhandler_serial::open): Ditto. * fhandler_tape.cc (fhandler_dev_tape::open): Ditto. * fhandler_tty.cc (fhandler_tty_slave::open): Ditto. (fhandler_pty_master::open): Ditto. * fhandler_windows.cc (fhandler_windows::open): Ditto. * fhandler_zero.cc (fhandler_dev_zero::open): Ditto. * fhandler_socket.cc (fhandler_socket::set_connect_secret): Accomodate new argument for open methods. * syscalls.cc (_open): Ditto. (stat_worker): Ditto.
2001-10-04 04:34:20 +02:00
fhandler_dev_mem::open (path_conv *, int flags, mode_t)
{
if (!wincap.has_physical_mem_access ())
{
set_errno (ENOENT);
debug_printf ("%s is accessible under NT/W2K only",
unit == 1 ? "/dev/mem" :
unit == 2 ? "/dev/kmem" :
"/dev/port");
return 0;
}
/* Check for illegal flags. */
if (flags & (O_APPEND | O_TRUNC | O_EXCL))
{
set_errno (EINVAL);
return 0;
}
UNICODE_STRING memstr;
RtlInitUnicodeString (&memstr, L"\\device\\physicalmemory");
OBJECT_ATTRIBUTES attr;
InitializeObjectAttributes (&attr, &memstr,
OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
NULL, NULL);
ACCESS_MASK section_access;
if ((flags & (O_RDONLY | O_WRONLY | O_RDWR)) == O_RDONLY)
{
set_access (GENERIC_READ);
section_access = SECTION_MAP_READ;
}
else if ((flags & (O_RDONLY | O_WRONLY | O_RDWR)) == O_WRONLY)
{
set_access (GENERIC_WRITE);
section_access = SECTION_MAP_READ | SECTION_MAP_WRITE;
}
else
{
set_access (GENERIC_READ | GENERIC_WRITE);
section_access = SECTION_MAP_READ | SECTION_MAP_WRITE;
}
HANDLE mem;
NTSTATUS ret = NtOpenSection (&mem, section_access, &attr);
if (!NT_SUCCESS (ret))
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
set_io_handle (NULL);
return 0;
}
set_io_handle (mem);
set_open_status ();
return 1;
}
int
fhandler_dev_mem::write (const void *ptr, size_t ulen)
{
if (!ulen || pos >= mem_size)
return 0;
if (!(get_access () & GENERIC_WRITE))
{
set_errno (EINVAL);
return -1;
}
if (pos + ulen > mem_size)
ulen = mem_size - pos;
PHYSICAL_ADDRESS phys;
NTSTATUS ret;
void *viewmem = NULL;
DWORD len = ulen + getpagesize () - 1;
phys.QuadPart = (ULONGLONG) pos;
if ((ret = NtMapViewOfSection (get_handle (),
INVALID_HANDLE_VALUE,
&viewmem,
0L,
len,
&phys,
&len,
ViewShare,
0,
PAGE_READONLY)) != STATUS_SUCCESS)
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
return -1;
}
memcpy ((char *) viewmem + (pos - phys.QuadPart), ptr, ulen);
if (!NT_SUCCESS (ret = NtUnmapViewOfSection (INVALID_HANDLE_VALUE, viewmem)))
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
return -1;
}
pos += ulen;
return ulen;
}
void __stdcall
fhandler_dev_mem::read (void *ptr, size_t& ulen)
{
if (!ulen || pos >= mem_size)
{
ulen = 0;
return;
}
if (!(get_access () & GENERIC_READ))
{
set_errno (EINVAL);
(ssize_t) ulen = -1;
return;
}
if (pos + ulen > mem_size)
ulen = mem_size - pos;
PHYSICAL_ADDRESS phys;
NTSTATUS ret;
void *viewmem = NULL;
DWORD len = ulen + getpagesize () - 1;
phys.QuadPart = (ULONGLONG) pos;
if ((ret = NtMapViewOfSection (get_handle (),
INVALID_HANDLE_VALUE,
&viewmem,
0L,
len,
&phys,
&len,
ViewShare,
0,
PAGE_READONLY)) != STATUS_SUCCESS)
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
(ssize_t) ulen = -1;
return;
}
memcpy (ptr, (char *) viewmem + (pos - phys.QuadPart), ulen);
if (!NT_SUCCESS (ret = NtUnmapViewOfSection (INVALID_HANDLE_VALUE, viewmem)))
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
(ssize_t) ulen = -1;
return;
}
pos += ulen;
return;
}
int
fhandler_dev_mem::close (void)
{
return fhandler_base::close ();
}
_off64_t
fhandler_dev_mem::lseek (_off64_t offset, int whence)
{
switch (whence)
{
case SEEK_SET:
pos = offset;
break;
case SEEK_CUR:
pos += offset;
break;
case SEEK_END:
pos = mem_size;
pos += offset;
break;
default:
set_errno (EINVAL);
return ILLEGAL_SEEK;
}
if (pos > mem_size)
{
set_errno (EINVAL);
return ILLEGAL_SEEK;
}
return pos;
}
HANDLE
fhandler_dev_mem::mmap (caddr_t *addr, size_t len, DWORD access,
int flags, _off64_t off)
{
* cygwin.din (fstat64): New symbol. (ftruncate64): Ditto. (lseek64): Ditto. (lstat64): Ditto. (mmap64): Ditto. (seekdir64): Ditto. (stat64): Ditto. (telldir64): Ditto. (truncate64): Ditto. * dir.cc (telldir64): New function. (telldir): Call telldir64(). (seekdir64): New function. (seekdir): Call seekdir64(). * fhandler.h: Redefine all methods using __off32_t to use __off64_t. * fhandler.cc: Use __off64_t and struct __stat64 throughout. * fhandler_clipboard.cc: Ditto. * fhandler_disk_file.cc: Ditto. * fhandler_dsp.cc: Ditto. * fhandler_floppy.cc: Ditto. * fhandler_mem.cc: Ditto. * fhandler_random.cc: Ditto. * fhandler_socket.cc: Ditto. * fhandler_tape.cc: Ditto. * fhandler_zero.cc: Ditto. * pipe.cc: Ditto. * glob.c: Ditto, call lstat64 and stat64 in Cygwin. * mmap.cc: Use __off64_t throughout. (mmap64): New function. * sec_acl.cc (acl_worker): Use struct __stat64, call stat64 and lstat64. * syscalls.cc (lseek64): New function. (stat64_to_stat32): Ditto. (fstat64): Ditto. (stat64): Ditto. (lstat64): Ditto. (ftruncate64): Ditto. (truncate64): Ditto. (_fstat): Call fstat64. (_stat): Call stat64. (cygwin_lstat): Rename to avoid declaration problem. Call lstat64. (stat_worker): Use struct __stat64. (access): Ditto. (ftruncate): Call ftruncate64. (truncate): Call truncate64. * wincap.cc: Set flag has_64bit_file_access appropriately. * wincap.h: Add flag has_64bit_file_access. * winsup.h (ILLEGAL_SEEK): Define as __off64_t. (stat_dev): Declare using struct __stat64. (stat_worker): Ditto. * include/cygwin/stat.h (struct __stat32): Define if compiling Cygwin. (struct __stat64): Ditto. (struct stat): Revert definition with explicitly sized datatypes. Eliminate sized field names. * include/cygwin/types.h (blksize_t): New type. (__blkcnt32_t): Ditto. (__blkcnt64_t): Ditto. (blkcnt_t): Ditto.
2002-02-25 18:47:51 +01:00
if (off >= mem_size
|| (DWORD) len >= mem_size
* cygwin.din (fstat64): New symbol. (ftruncate64): Ditto. (lseek64): Ditto. (lstat64): Ditto. (mmap64): Ditto. (seekdir64): Ditto. (stat64): Ditto. (telldir64): Ditto. (truncate64): Ditto. * dir.cc (telldir64): New function. (telldir): Call telldir64(). (seekdir64): New function. (seekdir): Call seekdir64(). * fhandler.h: Redefine all methods using __off32_t to use __off64_t. * fhandler.cc: Use __off64_t and struct __stat64 throughout. * fhandler_clipboard.cc: Ditto. * fhandler_disk_file.cc: Ditto. * fhandler_dsp.cc: Ditto. * fhandler_floppy.cc: Ditto. * fhandler_mem.cc: Ditto. * fhandler_random.cc: Ditto. * fhandler_socket.cc: Ditto. * fhandler_tape.cc: Ditto. * fhandler_zero.cc: Ditto. * pipe.cc: Ditto. * glob.c: Ditto, call lstat64 and stat64 in Cygwin. * mmap.cc: Use __off64_t throughout. (mmap64): New function. * sec_acl.cc (acl_worker): Use struct __stat64, call stat64 and lstat64. * syscalls.cc (lseek64): New function. (stat64_to_stat32): Ditto. (fstat64): Ditto. (stat64): Ditto. (lstat64): Ditto. (ftruncate64): Ditto. (truncate64): Ditto. (_fstat): Call fstat64. (_stat): Call stat64. (cygwin_lstat): Rename to avoid declaration problem. Call lstat64. (stat_worker): Use struct __stat64. (access): Ditto. (ftruncate): Call ftruncate64. (truncate): Call truncate64. * wincap.cc: Set flag has_64bit_file_access appropriately. * wincap.h: Add flag has_64bit_file_access. * winsup.h (ILLEGAL_SEEK): Define as __off64_t. (stat_dev): Declare using struct __stat64. (stat_worker): Ditto. * include/cygwin/stat.h (struct __stat32): Define if compiling Cygwin. (struct __stat64): Ditto. (struct stat): Revert definition with explicitly sized datatypes. Eliminate sized field names. * include/cygwin/types.h (blksize_t): New type. (__blkcnt32_t): Ditto. (__blkcnt64_t): Ditto. (blkcnt_t): Ditto.
2002-02-25 18:47:51 +01:00
|| off + len >= mem_size)
{
set_errno (EINVAL);
syscall_printf ("-1 = mmap(): illegal parameter, set EINVAL");
return INVALID_HANDLE_VALUE;
}
UNICODE_STRING memstr;
RtlInitUnicodeString (&memstr, L"\\device\\physicalmemory");
OBJECT_ATTRIBUTES attr;
InitializeObjectAttributes (&attr, &memstr,
OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
NULL, NULL);
ACCESS_MASK section_access;
ULONG protect;
if (access & FILE_MAP_COPY)
{
section_access = SECTION_MAP_READ | SECTION_MAP_WRITE;
protect = PAGE_WRITECOPY;
}
else if (access & FILE_MAP_WRITE)
{
section_access = SECTION_MAP_READ | SECTION_MAP_WRITE;
protect = PAGE_READWRITE;
}
else
{
section_access = SECTION_MAP_READ;
protect = PAGE_READONLY;
}
HANDLE h;
NTSTATUS ret = NtOpenSection (&h, section_access, &attr);
if (!NT_SUCCESS (ret))
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
syscall_printf ("-1 = mmap(): NtOpenSection failed with %E");
return INVALID_HANDLE_VALUE;
}
PHYSICAL_ADDRESS phys;
void *base = *addr;
DWORD dlen = len;
phys.QuadPart = (ULONGLONG) off;
if ((ret = NtMapViewOfSection (h,
INVALID_HANDLE_VALUE,
&base,
0L,
dlen,
&phys,
&dlen,
ViewShare /*??*/,
0,
protect)) != STATUS_SUCCESS)
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
syscall_printf ("-1 = mmap(): NtMapViewOfSection failed with %E");
return INVALID_HANDLE_VALUE;
}
if ((flags & MAP_FIXED) && base != *addr)
{
set_errno (EINVAL);
syscall_printf ("-1 = mmap(): address shift with MAP_FIXED given");
NtUnmapViewOfSection (INVALID_HANDLE_VALUE, base);
return INVALID_HANDLE_VALUE;
}
*addr = (caddr_t) base;
return h;
}
int
fhandler_dev_mem::munmap (HANDLE h, caddr_t addr, size_t len)
{
NTSTATUS ret;
if (!NT_SUCCESS (ret = NtUnmapViewOfSection (INVALID_HANDLE_VALUE, addr)))
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
return -1;
}
CloseHandle (h);
return 0;
}
int
fhandler_dev_mem::msync (HANDLE h, caddr_t addr, size_t len, int flags)
{
return 0;
}
BOOL
fhandler_dev_mem::fixup_mmap_after_fork (HANDLE h, DWORD access, DWORD offset,
DWORD size, void *address)
{
DWORD ret;
PHYSICAL_ADDRESS phys;
void *base = address;
DWORD dlen = size;
ULONG protect;
if (access & FILE_MAP_COPY)
protect = PAGE_WRITECOPY;
else if (access & FILE_MAP_WRITE)
protect = PAGE_READWRITE;
else
protect = PAGE_READONLY;
phys.QuadPart = (ULONGLONG) offset;
if ((ret = NtMapViewOfSection (h,
INVALID_HANDLE_VALUE,
&base,
0L,
dlen,
&phys,
&dlen,
ViewShare /*??*/,
0,
protect)) != STATUS_SUCCESS)
{
__seterrno_from_win_error (RtlNtStatusToDosError (ret));
syscall_printf ("-1 = fixup_mmap_after_fork(): NtMapViewOfSection failed with %E");
return FALSE;
}
return base == address;
}
int
* cygwin.din (fstat64): New symbol. (ftruncate64): Ditto. (lseek64): Ditto. (lstat64): Ditto. (mmap64): Ditto. (seekdir64): Ditto. (stat64): Ditto. (telldir64): Ditto. (truncate64): Ditto. * dir.cc (telldir64): New function. (telldir): Call telldir64(). (seekdir64): New function. (seekdir): Call seekdir64(). * fhandler.h: Redefine all methods using __off32_t to use __off64_t. * fhandler.cc: Use __off64_t and struct __stat64 throughout. * fhandler_clipboard.cc: Ditto. * fhandler_disk_file.cc: Ditto. * fhandler_dsp.cc: Ditto. * fhandler_floppy.cc: Ditto. * fhandler_mem.cc: Ditto. * fhandler_random.cc: Ditto. * fhandler_socket.cc: Ditto. * fhandler_tape.cc: Ditto. * fhandler_zero.cc: Ditto. * pipe.cc: Ditto. * glob.c: Ditto, call lstat64 and stat64 in Cygwin. * mmap.cc: Use __off64_t throughout. (mmap64): New function. * sec_acl.cc (acl_worker): Use struct __stat64, call stat64 and lstat64. * syscalls.cc (lseek64): New function. (stat64_to_stat32): Ditto. (fstat64): Ditto. (stat64): Ditto. (lstat64): Ditto. (ftruncate64): Ditto. (truncate64): Ditto. (_fstat): Call fstat64. (_stat): Call stat64. (cygwin_lstat): Rename to avoid declaration problem. Call lstat64. (stat_worker): Use struct __stat64. (access): Ditto. (ftruncate): Call ftruncate64. (truncate): Call truncate64. * wincap.cc: Set flag has_64bit_file_access appropriately. * wincap.h: Add flag has_64bit_file_access. * winsup.h (ILLEGAL_SEEK): Define as __off64_t. (stat_dev): Declare using struct __stat64. (stat_worker): Ditto. * include/cygwin/stat.h (struct __stat32): Define if compiling Cygwin. (struct __stat64): Ditto. (struct stat): Revert definition with explicitly sized datatypes. Eliminate sized field names. * include/cygwin/types.h (blksize_t): New type. (__blkcnt32_t): Ditto. (__blkcnt64_t): Ditto. (blkcnt_t): Ditto.
2002-02-25 18:47:51 +01:00
fhandler_dev_mem::fstat (struct __stat64 *buf, path_conv *pc)
{
fhandler_base::fstat (buf, pc);
buf->st_mode = S_IFCHR;
if (wincap.has_physical_mem_access ())
buf->st_mode |= S_IRUSR | S_IWUSR |
S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH;
buf->st_blksize = getpagesize ();
return 0;
}
int
fhandler_dev_mem::dup (fhandler_base *child)
{
int ret = fhandler_base::dup (child);
if (! ret)
{
fhandler_dev_mem *fhc = (fhandler_dev_mem *) child;
fhc->mem_size = mem_size;
fhc->pos = pos;
}
return ret;
}
void
fhandler_dev_mem::dump ()
{
paranoid_printf ("here, fhandler_dev_mem");
}