pipe.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "pipe.h"
  2. #include <common/spinlock.h>
  3. #include <process/process.h>
  4. #include <process/ptrace.h>
  5. #include <filesystem/VFS/VFS.h>
  6. #include <filesystem/fat32/fat32.h>
  7. #include <common/atomic.h>
  8. #include <mm/slab.h>
  9. #define MAX_PIPE_BUFF_SIZE 512
  10. struct pipe_t {
  11. volatile unsigned int valid_cnt;
  12. unsigned int read_pos;
  13. unsigned int write_pos;
  14. wait_queue_node_t read_wait_queue;
  15. wait_queue_node_t write_wait_queue;
  16. char buf[MAX_PIPE_BUFF_SIZE];
  17. spinlock_t lock;
  18. };
  19. long pipe_read(struct vfs_file_t *file_ptr, char *buf,
  20. int64_t count, long *position)
  21. {
  22. int i = 0;
  23. struct pipe_t *pipe_ptr = NULL;
  24. kdebug("pipe_read into!\n");
  25. pipe_ptr = (struct pipe_t *)file_ptr->private_data;
  26. spin_lock(&pipe_ptr->lock);
  27. while (pipe_ptr->valid_cnt == 0) {
  28. /* pipe 空 */
  29. kdebug("pipe_read empty!\n");
  30. wait_queue_wakeup(&pipe_ptr->write_wait_queue, PROC_UNINTERRUPTIBLE);
  31. wait_queue_sleep_on_unlock(&pipe_ptr->read_wait_queue, (void *)&pipe_ptr->lock);
  32. spin_lock(&pipe_ptr->lock);
  33. }
  34. for (i = 0; i < pipe_ptr->valid_cnt; i++) {
  35. if (i == count) {
  36. break;
  37. }
  38. copy_to_user(buf + i, &pipe_ptr->buf[pipe_ptr->read_pos], sizeof(char));
  39. pipe_ptr->read_pos = (pipe_ptr->read_pos + 1) % MAX_PIPE_BUFF_SIZE;
  40. }
  41. pipe_ptr->valid_cnt = pipe_ptr->valid_cnt - i;
  42. spin_unlock(&pipe_ptr->lock);
  43. wait_queue_wakeup(&pipe_ptr->write_wait_queue, PROC_UNINTERRUPTIBLE);
  44. kdebug("pipe_read end!\n");
  45. return i;
  46. }
  47. long pipe_write(struct vfs_file_t *file_ptr, char *buf,
  48. int64_t count, long *position)
  49. {
  50. int i = 0;
  51. struct pipe_t *pipe_ptr = NULL;
  52. kdebug("pipe_write into!\n");
  53. pipe_ptr = (struct pipe_t *)file_ptr->private_data;
  54. spin_lock(&pipe_ptr->lock);
  55. while (pipe_ptr->valid_cnt + count >= MAX_PIPE_BUFF_SIZE) {
  56. /* pipe 满 */
  57. kdebug("pipe_write pipe full!\n");
  58. wait_queue_wakeup(&pipe_ptr->read_wait_queue, PROC_UNINTERRUPTIBLE);
  59. wait_queue_sleep_on_unlock(&pipe_ptr->write_wait_queue, (void *)&pipe_ptr->lock);
  60. spin_lock(&pipe_ptr->lock);
  61. }
  62. for (i = pipe_ptr->valid_cnt; i < MAX_PIPE_BUFF_SIZE; i++) {
  63. if (i - pipe_ptr->valid_cnt == count) {
  64. break;
  65. }
  66. copy_from_user(&pipe_ptr->buf[pipe_ptr->write_pos], buf + i, sizeof(char));
  67. pipe_ptr->write_pos = (pipe_ptr->write_pos + 1) % MAX_PIPE_BUFF_SIZE;
  68. }
  69. pipe_ptr->valid_cnt += count;
  70. spin_unlock(&pipe_ptr->lock);
  71. wait_queue_wakeup(&pipe_ptr->read_wait_queue, PROC_UNINTERRUPTIBLE);
  72. kdebug("pipe_write out!\n");
  73. return count;
  74. }
  75. long pipe_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  76. {
  77. return 0;
  78. }
  79. struct vfs_file_operations_t g_pipe_file_ops = {
  80. .open = NULL,
  81. .close = pipe_close,
  82. .read = pipe_read,
  83. .write = pipe_write,
  84. .lseek = NULL,
  85. .ioctl = NULL,
  86. .readdir = NULL,
  87. };
  88. static struct pipe_t *pipe_alloc()
  89. {
  90. struct pipe_t *pipe_ptr = NULL;
  91. pipe_ptr = (struct pipe_t *)kmalloc(sizeof(struct pipe_t), 0);
  92. spin_init(&pipe_ptr->lock);
  93. pipe_ptr->read_pos = 0;
  94. pipe_ptr->write_pos = 0;
  95. pipe_ptr->valid_cnt = 0;
  96. memset(pipe_ptr->buf, 0, MAX_PIPE_BUFF_SIZE);
  97. wait_queue_init(&pipe_ptr->read_wait_queue, NULL);
  98. wait_queue_init(&pipe_ptr->write_wait_queue, NULL);
  99. return pipe_ptr;
  100. }
  101. /**
  102. * @brief 创建管道
  103. *
  104. * @param fd(r8) 文件句柄指针
  105. * @param num(r9) 文件句柄个数
  106. * @return uint64_t
  107. */
  108. uint64_t sys_pipe(struct pt_regs *regs)
  109. {
  110. int *fd = NULL;
  111. struct pipe_t *pipe_ptr = NULL;
  112. struct vfs_file_t *read_file = NULL;
  113. struct vfs_file_t *write_file = NULL;
  114. fd = (int *)regs->r8;
  115. kdebug("pipe creat into!\n");
  116. /* step1 申请pipe结构体、初始化 */
  117. pipe_ptr = pipe_alloc();
  118. /* step2 申请2个fd文件句柄,1个作为读端、1个作为写端 */
  119. read_file = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
  120. memset(read_file, 0, sizeof(struct vfs_file_t));
  121. fd[0] = process_fd_alloc(read_file);
  122. if (fd[0] == -1) {
  123. kdebug("pipe alloc read fd fail!\n");
  124. kfree(pipe_ptr);
  125. kfree(read_file);
  126. return -1;
  127. }
  128. write_file = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
  129. memset(write_file, 0, sizeof(struct vfs_file_t));
  130. fd[1] = process_fd_alloc(write_file);
  131. if (fd[1] == -1) {
  132. kdebug("pipe alloc write fd fail!\n");
  133. kfree(pipe_ptr);
  134. kfree(read_file);
  135. kfree(write_file);
  136. return -1;
  137. }
  138. /* step3 绑定pipe和file */
  139. read_file->private_data = (void *)pipe_ptr;
  140. read_file->file_ops = &g_pipe_file_ops;
  141. read_file->mode = ATTR_READ_ONLY;
  142. write_file->private_data = (void *)pipe_ptr;
  143. write_file->file_ops = &g_pipe_file_ops;
  144. write_file->mode = O_WRONLY;
  145. kdebug("pipe creat end!\n");
  146. return 0;
  147. }