mem_user.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include <errno.h>
  6. #include <sys/mman.h>
  7. #include "mem_user.h"
  8. #include "mem.h"
  9. #include "user.h"
  10. #include "os.h"
  11. #include "proc_mm.h"
  12. void map(int fd, unsigned long virt, unsigned long len, int r, int w,
  13. int x, int phys_fd, unsigned long long offset)
  14. {
  15. struct proc_mm_op map;
  16. int prot, n;
  17. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  18. (x ? PROT_EXEC : 0);
  19. map = ((struct proc_mm_op) { .op = MM_MMAP,
  20. .u =
  21. { .mmap =
  22. { .addr = virt,
  23. .len = len,
  24. .prot = prot,
  25. .flags = MAP_SHARED |
  26. MAP_FIXED,
  27. .fd = phys_fd,
  28. .offset = offset
  29. } } } );
  30. n = os_write_file(fd, &map, sizeof(map));
  31. if(n != sizeof(map))
  32. printk("map : /proc/mm map failed, err = %d\n", -n);
  33. }
  34. int unmap(int fd, void *addr, unsigned long len)
  35. {
  36. struct proc_mm_op unmap;
  37. int n;
  38. unmap = ((struct proc_mm_op) { .op = MM_MUNMAP,
  39. .u =
  40. { .munmap =
  41. { .addr = (unsigned long) addr,
  42. .len = len } } } );
  43. n = os_write_file(fd, &unmap, sizeof(unmap));
  44. if(n != sizeof(unmap)) {
  45. if(n < 0)
  46. return(n);
  47. else if(n > 0)
  48. return(-EIO);
  49. }
  50. return(0);
  51. }
  52. int protect(int fd, unsigned long addr, unsigned long len, int r, int w,
  53. int x, int must_succeed)
  54. {
  55. struct proc_mm_op protect;
  56. int prot, n;
  57. prot = (r ? PROT_READ : 0) | (w ? PROT_WRITE : 0) |
  58. (x ? PROT_EXEC : 0);
  59. protect = ((struct proc_mm_op) { .op = MM_MPROTECT,
  60. .u =
  61. { .mprotect =
  62. { .addr = (unsigned long) addr,
  63. .len = len,
  64. .prot = prot } } } );
  65. n = os_write_file(fd, &protect, sizeof(protect));
  66. if(n != sizeof(protect)) {
  67. if(n == 0) return(0);
  68. if(must_succeed)
  69. panic("protect failed, err = %d", -n);
  70. return(-EIO);
  71. }
  72. return(0);
  73. }
  74. void before_mem_skas(unsigned long unused)
  75. {
  76. }
  77. /*
  78. * Overrides for Emacs so that we follow Linus's tabbing style.
  79. * Emacs will notice this stuff at the end of the file and automatically
  80. * adjust the settings for this buffer only. This must remain at the end
  81. * of the file.
  82. * ---------------------------------------------------------------------------
  83. * Local variables:
  84. * c-file-style: "linux"
  85. * End:
  86. */