pipe.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. struct pipe_data_t
  10. {
  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. spinlock_t lock;
  17. } __attribute__((packed));
  18. // 由于kmalloc分配的内存是按照2^n对齐的,因此我们需要这样来确定pipe的buffer大小以消除内部碎片
  19. // 我们设定pipe的总大小为1024字节
  20. #define PIPE_BUFF_SIZE (1024 - sizeof(struct pipe_data_t))
  21. struct pipe_t
  22. {
  23. struct pipe_data_t data;
  24. char buf[PIPE_BUFF_SIZE];
  25. };
  26. long pipe_read(struct vfs_file_t *file_ptr, char *buf,
  27. int64_t count, long *position)
  28. {
  29. int i = 0;
  30. struct pipe_t *pipe_ptr = NULL;
  31. kdebug("pipe_read into!\n");
  32. pipe_ptr = (struct pipe_t *)file_ptr->private_data;
  33. spin_lock(&pipe_ptr->data.lock);
  34. while (pipe_ptr->data.valid_cnt == 0)
  35. {
  36. /* pipe 空 */
  37. kdebug("pipe_read empty!\n");
  38. wait_queue_wakeup(&pipe_ptr->data.write_wait_queue, PROC_UNINTERRUPTIBLE);
  39. wait_queue_sleep_on_unlock(&pipe_ptr->data.read_wait_queue, (void *)&pipe_ptr->data.lock);
  40. spin_lock(&pipe_ptr->data.lock);
  41. }
  42. for (i = 0; i < pipe_ptr->data.valid_cnt; i++)
  43. {
  44. if (i == count)
  45. {
  46. break;
  47. }
  48. copy_to_user(buf + i, &pipe_ptr->buf[pipe_ptr->data.read_pos], sizeof(char));
  49. pipe_ptr->data.read_pos = (pipe_ptr->data.read_pos + 1) % PIPE_BUFF_SIZE;
  50. }
  51. pipe_ptr->data.valid_cnt = pipe_ptr->data.valid_cnt - i;
  52. spin_unlock(&pipe_ptr->data.lock);
  53. wait_queue_wakeup(&pipe_ptr->data.write_wait_queue, PROC_UNINTERRUPTIBLE);
  54. kdebug("pipe_read end!\n");
  55. return i;
  56. }
  57. long pipe_write(struct vfs_file_t *file_ptr, char *buf,
  58. int64_t count, long *position)
  59. {
  60. int i = 0;
  61. struct pipe_t *pipe_ptr = NULL;
  62. kdebug("pipe_write into!\n");
  63. pipe_ptr = (struct pipe_t *)file_ptr->private_data;
  64. spin_lock(&pipe_ptr->data.lock);
  65. while (pipe_ptr->data.valid_cnt + count >= PIPE_BUFF_SIZE)
  66. {
  67. /* pipe 满 */
  68. kdebug("pipe_write pipe full!\n");
  69. wait_queue_wakeup(&pipe_ptr->data.read_wait_queue, PROC_UNINTERRUPTIBLE);
  70. wait_queue_sleep_on_unlock(&pipe_ptr->data.write_wait_queue, (void *)&pipe_ptr->data.lock);
  71. spin_lock(&pipe_ptr->data.lock);
  72. }
  73. for (i = pipe_ptr->data.valid_cnt; i < PIPE_BUFF_SIZE; i++)
  74. {
  75. if (i - pipe_ptr->data.valid_cnt == count)
  76. {
  77. break;
  78. }
  79. copy_from_user(&pipe_ptr->buf[pipe_ptr->data.write_pos], buf + i, sizeof(char));
  80. pipe_ptr->data.write_pos = (pipe_ptr->data.write_pos + 1) % PIPE_BUFF_SIZE;
  81. }
  82. pipe_ptr->data.valid_cnt += count;
  83. spin_unlock(&pipe_ptr->data.lock);
  84. wait_queue_wakeup(&pipe_ptr->data.read_wait_queue, PROC_UNINTERRUPTIBLE);
  85. kdebug("pipe_write out!\n");
  86. return count;
  87. }
  88. long pipe_close(struct vfs_index_node_t *inode, struct vfs_file_t *file_ptr)
  89. {
  90. return 0;
  91. }
  92. struct vfs_file_operations_t g_pipe_file_ops = {
  93. .open = NULL,
  94. .close = pipe_close,
  95. .read = pipe_read,
  96. .write = pipe_write,
  97. .lseek = NULL,
  98. .ioctl = NULL,
  99. .readdir = NULL,
  100. };
  101. static struct pipe_t *pipe_alloc()
  102. {
  103. struct pipe_t *pipe_ptr = NULL;
  104. pipe_ptr = (struct pipe_t *)kzalloc(sizeof(struct pipe_t), 0);
  105. spin_init(&pipe_ptr->data.lock);
  106. pipe_ptr->data.read_pos = 0;
  107. pipe_ptr->data.write_pos = 0;
  108. pipe_ptr->data.valid_cnt = 0;
  109. memset(pipe_ptr->buf, 0, PIPE_BUFF_SIZE);
  110. wait_queue_init(&pipe_ptr->data.read_wait_queue, NULL);
  111. wait_queue_init(&pipe_ptr->data.write_wait_queue, NULL);
  112. return pipe_ptr;
  113. }
  114. /**
  115. * @brief 创建管道
  116. *
  117. * @param fd(r8) 文件句柄指针
  118. * @param num(r9) 文件句柄个数
  119. * @return uint64_t
  120. */
  121. uint64_t sys_pipe(struct pt_regs *regs)
  122. {
  123. int *fd = NULL;
  124. struct pipe_t *pipe_ptr = NULL;
  125. struct vfs_file_t *read_file = NULL;
  126. struct vfs_file_t *write_file = NULL;
  127. fd = (int *)regs->r8;
  128. kdebug("pipe creat into!\n");
  129. /* step1 申请pipe结构体、初始化 */
  130. pipe_ptr = pipe_alloc();
  131. /* step2 申请2个fd文件句柄,1个作为读端、1个作为写端 */
  132. read_file = (struct vfs_file_t *)kzalloc(sizeof(struct vfs_file_t), 0);
  133. fd[0] = process_fd_alloc(read_file);
  134. if (fd[0] == -1)
  135. {
  136. kdebug("pipe alloc read fd fail!\n");
  137. kfree(pipe_ptr);
  138. kfree(read_file);
  139. return -1;
  140. }
  141. write_file = (struct vfs_file_t *)kzalloc(sizeof(struct vfs_file_t), 0);
  142. fd[1] = process_fd_alloc(write_file);
  143. if (fd[1] == -1)
  144. {
  145. kdebug("pipe alloc write fd fail!\n");
  146. kfree(pipe_ptr);
  147. kfree(read_file);
  148. kfree(write_file);
  149. return -1;
  150. }
  151. /* step3 绑定pipe和file */
  152. read_file->private_data = (void *)pipe_ptr;
  153. read_file->file_ops = &g_pipe_file_ops;
  154. read_file->mode = VFS_FILE_MODE_READ;
  155. write_file->private_data = (void *)pipe_ptr;
  156. write_file->file_ops = &g_pipe_file_ops;
  157. write_file->mode = VFS_FILE_MODE_WRITE;
  158. kdebug("pipe creat end!\n");
  159. return 0;
  160. }