Chinaunix首页 | 论坛 | 博客
  • 博客访问: 232908
  • 博文数量: 37
  • 博客积分: 933
  • 博客等级: 军士长
  • 技术积分: 511
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-16 10:15
文章分类
文章存档

2012年(1)

2011年(36)

分类: LINUX

2011-03-29 13:54:37

在看 代码时发现,如果设置了MAP_FIXED标志,函数就做一些极其简单的检查,然后就把你指定的addr返回给你,代表你查找的空闲的线性区找到了。
  1. unsigned long
  2. 1371 get_unmapped_area_prot(struct file *file, unsigned long addr, unsigned long len,
  3.      /* */
  4. 1372 unsigned long pgoff, unsigned long flags, int exec)
  5. 1373 {
  6. 1374 unsigned long ret;
  7. 1375
  8. 1376 if (!(flags & MAP_FIXED)) {
  9. 1377 unsigned long (*get_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
  10. 1378
  11. 1379 if (exec && current->mm->get_unmapped_exec_area)
  12. 1380 get_area = current->mm->get_unmapped_exec_area;
  13. 1381 else
  14. 1382 get_area = current->mm->get_unmapped_area;
  15. 1383
  16. 1384 if (file && file->f_op && file->f_op->get_unmapped_area)
  17. 1385 get_area = file->f_op->get_unmapped_area;
  18. 1386 addr = get_area(file, addr, len, pgoff, flags);
  19. 1387 if (IS_ERR_VALUE(addr))
  20. 1388 return addr;
  21. 1389 }
  22. 1390
  23. 1391 if (addr > TASK_SIZE - len)
  24. 1392 return -ENOMEM;
  25. 1393 if (addr & ~PAGE_MASK)
  26. 1394 return -EINVAL;
  27. 1395 if (file && is_file_hugepages(file)) {
  28. 1396 /*
  29. 1397 * Check if the given range is hugepage aligned, and
  30. 1398 * can be made suitable for hugepages.
  31. 1399 */
  32. 1400 ret = prepare_hugepage_range(addr, len, pgoff);
  33. 1401 } else {
  34. 1402 /*
  35. 1403 * Ensure that a normal request is not falling in a
  36. 1404 * reserved hugepage range. For some archs like IA-64,
  37. 1405 * there is a separate region for hugepages.
  38. 1406 */
  39. 1407 ret = is_hugepage_only_range(current->mm, addr, len);
  40. 1408 }
  41. 1409 if (ret)
  42. 1410 return -EINVAL;
  43. 1411 return addr;
  44. 1412 }
着重看一下后半部分if(0)....
 
1)首先检查addr+len是否超过了用户空间
2)所请求的地址是否是页对齐的
由于我们先不考虑文件映射 所以后面的暂时忽略
到这里你可能会想,如果你指定的地址开始的线性区真好被映射了怎么办呢?那不是应该返回NULL吗?
然而,事实是MAP_FIXED标志的一个特性是:
如果你指定的地址和已有的线性区重叠,那么就抛弃已有的线性区映射。cool,but dangerous.....
 
MAP_FIXED Do not select a different address than the one specified. If the memory region specified by start and len overlaps pages of any existing mapping(s), then the overlapped part of the existing mapping(s) will be discarded. If the specified address cannot be used, mmap() will fail. If MAP_FIXED is specified, start must be a multiple of the page size. Use of this option is discouraged.
阅读(6275) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~