mem_user.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * arch/um/kernel/mem_user.c
  3. *
  4. * BRIEF MODULE DESCRIPTION
  5. * user side memory routines for supporting IO memory inside user mode linux
  6. *
  7. * Copyright (C) 2001 RidgeRun, Inc.
  8. * Author: RidgeRun, Inc.
  9. * Greg Lonnon glonnon@ridgerun.com or info@ridgerun.com
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the
  13. * Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  19. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * You should have received a copy of the GNU General Public License along
  28. * with this program; if not, write to the Free Software Foundation, Inc.,
  29. * 675 Mass Ave, Cambridge, MA 02139, USA.
  30. */
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <stddef.h>
  34. #include <stdarg.h>
  35. #include <unistd.h>
  36. #include <errno.h>
  37. #include <string.h>
  38. #include <fcntl.h>
  39. #include <sys/types.h>
  40. #include <sys/mman.h>
  41. #include "kern_util.h"
  42. #include "user.h"
  43. #include "user_util.h"
  44. #include "mem_user.h"
  45. #include "init.h"
  46. #include "os.h"
  47. #include "tempfile.h"
  48. #include "kern_constants.h"
  49. #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
  50. static int create_tmp_file(unsigned long len)
  51. {
  52. int fd, err;
  53. char zero;
  54. fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
  55. if(fd < 0) {
  56. os_print_error(fd, "make_tempfile");
  57. exit(1);
  58. }
  59. err = os_mode_fd(fd, 0777);
  60. if(err < 0){
  61. os_print_error(err, "os_mode_fd");
  62. exit(1);
  63. }
  64. err = os_seek_file(fd, len);
  65. if(err < 0){
  66. os_print_error(err, "os_seek_file");
  67. exit(1);
  68. }
  69. zero = 0;
  70. err = os_write_file(fd, &zero, 1);
  71. if(err != 1){
  72. os_print_error(err, "os_write_file");
  73. exit(1);
  74. }
  75. return(fd);
  76. }
  77. void check_tmpexec(void)
  78. {
  79. void *addr;
  80. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  81. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  82. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  83. printf("Checking PROT_EXEC mmap in /tmp...");
  84. fflush(stdout);
  85. if(addr == MAP_FAILED){
  86. err = errno;
  87. perror("failed");
  88. if(err == EPERM)
  89. printf("/tmp must be not mounted noexec\n");
  90. exit(1);
  91. }
  92. printf("OK\n");
  93. munmap(addr, UM_KERN_PAGE_SIZE);
  94. os_close_file(fd);
  95. }
  96. static int have_devanon = 0;
  97. void check_devanon(void)
  98. {
  99. int fd;
  100. printk("Checking for /dev/anon on the host...");
  101. fd = open("/dev/anon", O_RDWR);
  102. if(fd < 0){
  103. printk("Not available (open failed with errno %d)\n", errno);
  104. return;
  105. }
  106. printk("OK\n");
  107. have_devanon = 1;
  108. }
  109. static int create_anon_file(unsigned long len)
  110. {
  111. void *addr;
  112. int fd;
  113. fd = open("/dev/anon", O_RDWR);
  114. if(fd < 0) {
  115. os_print_error(fd, "opening /dev/anon");
  116. exit(1);
  117. }
  118. addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  119. if(addr == MAP_FAILED){
  120. perror("mapping physmem file");
  121. exit(1);
  122. }
  123. munmap(addr, len);
  124. return(fd);
  125. }
  126. int create_mem_file(unsigned long len)
  127. {
  128. int err, fd;
  129. if(have_devanon)
  130. fd = create_anon_file(len);
  131. else fd = create_tmp_file(len);
  132. err = os_set_exec_close(fd, 1);
  133. if(err < 0)
  134. os_print_error(err, "exec_close");
  135. return(fd);
  136. }
  137. struct iomem_region *iomem_regions = NULL;
  138. int iomem_size = 0;
  139. static int __init parse_iomem(char *str, int *add)
  140. {
  141. struct iomem_region *new;
  142. struct uml_stat buf;
  143. char *file, *driver;
  144. int fd, err, size;
  145. driver = str;
  146. file = strchr(str,',');
  147. if(file == NULL){
  148. printf("parse_iomem : failed to parse iomem\n");
  149. goto out;
  150. }
  151. *file = '\0';
  152. file++;
  153. fd = os_open_file(file, of_rdwr(OPENFLAGS()), 0);
  154. if(fd < 0){
  155. os_print_error(fd, "parse_iomem - Couldn't open io file");
  156. goto out;
  157. }
  158. err = os_stat_fd(fd, &buf);
  159. if(err < 0){
  160. os_print_error(err, "parse_iomem - cannot stat_fd file");
  161. goto out_close;
  162. }
  163. new = malloc(sizeof(*new));
  164. if(new == NULL){
  165. perror("Couldn't allocate iomem_region struct");
  166. goto out_close;
  167. }
  168. size = (buf.ust_size + UM_KERN_PAGE_SIZE) & ~(UM_KERN_PAGE_SIZE - 1);
  169. *new = ((struct iomem_region) { .next = iomem_regions,
  170. .driver = driver,
  171. .fd = fd,
  172. .size = size,
  173. .phys = 0,
  174. .virt = 0 });
  175. iomem_regions = new;
  176. iomem_size += new->size + UM_KERN_PAGE_SIZE;
  177. return(0);
  178. out_close:
  179. os_close_file(fd);
  180. out:
  181. return(1);
  182. }
  183. __uml_setup("iomem=", parse_iomem,
  184. "iomem=<name>,<file>\n"
  185. " Configure <file> as an IO memory region named <name>.\n\n"
  186. );
  187. int protect_memory(unsigned long addr, unsigned long len, int r, int w, int x,
  188. int must_succeed)
  189. {
  190. int err;
  191. err = os_protect_memory((void *) addr, len, r, w, x);
  192. if(err < 0){
  193. if(must_succeed)
  194. panic("protect failed, err = %d", -err);
  195. else return(err);
  196. }
  197. return(0);
  198. }
  199. #if 0
  200. /* Debugging facility for dumping stuff out to the host, avoiding the timing
  201. * problems that come with printf and breakpoints.
  202. * Enable in case of emergency.
  203. */
  204. int logging = 1;
  205. int logging_fd = -1;
  206. int logging_line = 0;
  207. char logging_buf[512];
  208. void log(char *fmt, ...)
  209. {
  210. va_list ap;
  211. struct timeval tv;
  212. struct openflags flags;
  213. if(logging == 0) return;
  214. if(logging_fd < 0){
  215. flags = of_create(of_trunc(of_rdwr(OPENFLAGS())));
  216. logging_fd = os_open_file("log", flags, 0644);
  217. }
  218. gettimeofday(&tv, NULL);
  219. sprintf(logging_buf, "%d\t %u.%u ", logging_line++, tv.tv_sec,
  220. tv.tv_usec);
  221. va_start(ap, fmt);
  222. vsprintf(&logging_buf[strlen(logging_buf)], fmt, ap);
  223. va_end(ap);
  224. write(logging_fd, logging_buf, strlen(logging_buf));
  225. }
  226. #endif
  227. /*
  228. * Overrides for Emacs so that we follow Linus's tabbing style.
  229. * Emacs will notice this stuff at the end of the file and automatically
  230. * adjust the settings for this buffer only. This must remain at the end
  231. * of the file.
  232. * ---------------------------------------------------------------------------
  233. * Local variables:
  234. * c-file-style: "linux"
  235. * End:
  236. */