user.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2023 Agustina Arzille.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Definitions for userspace.
  18. */
  19. #ifndef KERN_USER_H
  20. #define KERN_USER_H
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <machine/pmap.h>
  24. #include <kern/task.h>
  25. struct ipc_iov_iter;
  26. // Type used for fast, unaligned access.
  27. union user_ua
  28. {
  29. unsigned u2 __attribute__ ((mode (HI)));
  30. unsigned u4 __attribute__ ((mode (SI)));
  31. unsigned u8 __attribute__ ((mode (DI)));
  32. } __packed;
  33. // Test that an address is accessible for the current task.
  34. static inline bool
  35. user_check_range (const void *addr, size_t size)
  36. {
  37. return (
  38. #if PMAP_START_ADDRESS > 0
  39. (uintptr_t)addr >= PMAP_START_ADDRESS &&
  40. #else
  41. 1 &&
  42. #endif
  43. ((uintptr_t)addr + size < PMAP_END_ADDRESS));
  44. }
  45. // Copy bytes to userspace.
  46. int user_copy_to (void *udst, const void *src, size_t size);
  47. // Copy bytes from userspace.
  48. int user_copy_from (void *dst, const void *usrc, size_t size);
  49. // Same as above, only these operate on iovecs.
  50. ssize_t user_copyv_to (struct ipc_iov_iter *udst, struct ipc_iov_iter *src);
  51. ssize_t user_copyv_from (struct ipc_iov_iter *dst, struct ipc_iov_iter *usrc);
  52. // Test that a sized structure is accessible for the current task.
  53. bool user_check_struct (const void *uptr, size_t min_size);
  54. // Copy to/from userspace a struct that knows its size.
  55. int user_read_struct (void *dst, const void *usrc, size_t size);
  56. int user_write_struct (void *udst, const void *src, size_t size);
  57. #endif