vm-allocate.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  4. *
  5. * This file is part of GNU Mes.
  6. *
  7. * GNU Mes is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * GNU Mes is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <gnu/syscall.h>
  21. struct mach_msg_int_address
  22. {
  23. mach_msg_header_t header;
  24. mach_msg_type_t type_one; int one;
  25. mach_msg_type_t type_two; vm_address_t two;
  26. };
  27. struct mach_msg_address_int_int
  28. {
  29. mach_msg_header_t header;
  30. mach_msg_type_t type_one; vm_address_t one;
  31. mach_msg_type_t type_two; vm_size_t two;
  32. mach_msg_type_t type_three; boolean_t three;
  33. };
  34. const mach_msg_type_t mach_msg_type_boolean =
  35. {
  36. (unsigned char) MACH_MSG_TYPE_BOOLEAN, // msgt_name
  37. 32, // msgt_size
  38. 1, // msgt_number
  39. 1, // msgt_inline
  40. 0, // msgt_longform
  41. 0, // msgt_deallocate
  42. 0 // msgt_unused
  43. };
  44. kern_return_t
  45. __vm_allocate (mach_port_t task, vm_address_t *address, vm_size_t size, boolean_t anywhere)
  46. {
  47. union message
  48. {
  49. struct mach_msg_address_int_int request;
  50. struct mach_msg_int_address reply;
  51. };
  52. union message message = {0};
  53. message.request.header.msgh_size = sizeof (message.request);
  54. message.request.type_one = mach_msg_type_int32;
  55. message.request.one = *address;
  56. message.request.type_two = mach_msg_type_int32;
  57. message.request.two = size;
  58. message.request.type_three = mach_msg_type_boolean;
  59. message.request.three = anywhere;
  60. kern_return_t result = __syscall_get (task, SYS__vm_allocate,
  61. &message.request.header,
  62. sizeof (message.reply));
  63. if (message.reply.one != KERN_SUCCESS)
  64. return message.reply.one;
  65. if (result == KERN_SUCCESS)
  66. *address = message.reply.two;
  67. return result;
  68. }