| 1 |
/* |
| 2 |
* Copyright (C) 2006-2007 XenSource Ltd. |
| 3 |
* Copyright (C) 2008 Citrix Ltd. |
| 4 |
* Author Vincent Hanquez <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as published |
| 8 |
* by the Free Software Foundation; version 2.1 only. with the special |
| 9 |
* exception on linking described in file LICENSE. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU Lesser General Public License for more details. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include <unistd.h> |
| 18 |
#include <stdlib.h> |
| 19 |
#include <sys/mman.h> |
| 20 |
#include <string.h> |
| 21 |
#include <errno.h> |
| 22 |
#include "mmap_stubs.h" |
| 23 |
|
| 24 |
#include <xen-tools/common-macros.h> |
| 25 |
|
| 26 |
#include <caml/mlvalues.h> |
| 27 |
#include <caml/memory.h> |
| 28 |
#include <caml/alloc.h> |
| 29 |
#include <caml/custom.h> |
| 30 |
#include <caml/fail.h> |
| 31 |
#include <caml/callback.h> |
| 32 |
|
| 33 |
#define Intf_val(a) ((struct mmap_interface *)Data_abstract_val(a)) |
| 34 |
|
| 35 |
static int mmap_interface_init(struct mmap_interface *intf, |
| 36 |
int fd, int pflag, int mflag, |
| 37 |
int len, int offset) |
| 38 |
{ |
| 39 |
intf->len = len; |
| 40 |
intf->addr = mmap(NULL, len, pflag, mflag, fd, offset); |
| 41 |
return (intf->addr == MAP_FAILED) ? errno : 0; |
| 42 |
} |
| 43 |
|
| 44 |
CAMLprim value stub_mmap_init(value fd, value pflag, value mflag, |
| 45 |
value len, value offset) |
| 46 |
{ |
| 47 |
CAMLparam5(fd, pflag, mflag, len, offset); |
| 48 |
CAMLlocal1(result); |
| 49 |
int c_pflag, c_mflag; |
| 50 |
|
| 51 |
switch (Int_val(pflag)) { |
| 52 |
case 0: c_pflag = PROT_READ; break; |
| 53 |
case 1: c_pflag = PROT_WRITE; break; |
| 54 |
case 2: c_pflag = PROT_READ|PROT_WRITE; break; |
| 55 |
default: caml_invalid_argument("protectiontype"); |
| 56 |
} |
| 57 |
|
| 58 |
switch (Int_val(mflag)) { |
| 59 |
case 0: c_mflag = MAP_SHARED; break; |
| 60 |
case 1: c_mflag = MAP_PRIVATE; break; |
| 61 |
default: caml_invalid_argument("maptype"); |
| 62 |
} |
| 63 |
|
| 64 |
BUILD_BUG_ON((sizeof(struct mmap_interface) % sizeof(value)) != 0); |
| 65 |
result = caml_alloc(Wsize_bsize(sizeof(struct mmap_interface)), |
| 66 |
Abstract_tag); |
| 67 |
|
| 68 |
if (mmap_interface_init(Intf_val(result), Int_val(fd), |
| 69 |
c_pflag, c_mflag, |
| 70 |
Int_val(len), Int_val(offset))) |
| 71 |
caml_failwith("mmap"); |
| 72 |
CAMLreturn(result); |
| 73 |
} |
| 74 |
|
| 75 |
CAMLprim value stub_mmap_final(value intf) |
| 76 |
{ |
| 77 |
CAMLparam1(intf); |
| 78 |
|
| 79 |
if (Intf_val(intf)->addr != MAP_FAILED) |
| 80 |
munmap(Intf_val(intf)->addr, Intf_val(intf)->len); |
| 81 |
Intf_val(intf)->addr = MAP_FAILED; |
| 82 |
|
| 83 |
CAMLreturn(Val_unit); |
| 84 |
} |
| 85 |
|
| 86 |
CAMLprim value stub_mmap_read(value intf, value start, value len) |
| 87 |
{ |
| 88 |
CAMLparam3(intf, start, len); |
| 89 |
CAMLlocal1(data); |
| 90 |
int c_start; |
| 91 |
int c_len; |
| 92 |
|
| 93 |
c_start = Int_val(start); |
| 94 |
c_len = Int_val(len); |
| 95 |
|
| 96 |
if (c_start > Intf_val(intf)->len) |
| 97 |
caml_invalid_argument("start invalid"); |
| 98 |
if (c_start + c_len > Intf_val(intf)->len) |
| 99 |
caml_invalid_argument("len invalid"); |
| 100 |
|
| 101 |
data = caml_alloc_string(c_len); |
| 102 |
memcpy((char *)String_val(data), Intf_val(intf)->addr + c_start, c_len); |
| 103 |
|
| 104 |
CAMLreturn(data); |
| 105 |
} |
| 106 |
|
| 107 |
CAMLprim value stub_mmap_write(value intf, value data, value start) |
| 108 |
{ |
| 109 |
CAMLparam3(intf, data, start); |
| 110 |
int c_start; |
| 111 |
int c_len; |
| 112 |
|
| 113 |
c_start = Int_val(start); |
| 114 |
c_len = caml_string_length(data); |
| 115 |
|
| 116 |
if (c_start > Intf_val(intf)->len) |
| 117 |
caml_invalid_argument("start invalid"); |
| 118 |
if (c_start + c_len > Intf_val(intf)->len) |
| 119 |
caml_invalid_argument("len invalid"); |
| 120 |
|
| 121 |
memcpy(Intf_val(intf)->addr + c_start, String_val(data), c_len); |
| 122 |
|
| 123 |
CAMLreturn(Val_unit); |
| 124 |
} |
| 125 |
|
| 126 |
CAMLprim value stub_mmap_getpagesize(value unit) |
| 127 |
{ |
| 128 |
CAMLparam1(unit); |
| 129 |
CAMLlocal1(data); |
| 130 |
|
| 131 |
data = Val_int(getpagesize()); |
| 132 |
CAMLreturn(data); |
| 133 |
} |