какие вызовы ядра нужны для реализации fread и feof, как в этом примере?
http://www.c-cpp.ru/content/fread

ещё нужны fwrite и fflush (для вывода).

файлы открывать и закрывать не нужно, потому что есть уже открытые дескрипторы, и их номера известны (0 - вход, 1 - выход).

http://man7.org/linux/man-pages/man2/read.2.html
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
[html]<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html">http://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html</a>[/html]

про файл unistd.h - https://en.wikipedia.org/wiki/Unistd.h

https://www.kernel.org/doc/htmldocs/kernel-api/
иии, функции read там не видно! А она должна где-то быть точно.
(ну, то есть, понятно, что она в стандартной библиотеке, но не читать же её исходники, чтобы понять какими вызовами ядра она реализуется, это должно быть в документации, которую надо найти)

https://en.wikipedia.org/wiki/Read_(system_call)
https://en.wikipedia.org/wiki/Write_(system_call)

Что нам предлагает поиск в интернете?

Код:
_read_write:
  mov rax, SYS_READ
  mov rdi, [fd]
  mov rsi, file_buffer
  mov rdx, BUFFER_SIZE
  syscall

https://syscalls.kernelgrok.com/

Имя

eax

ebx

ecx

edx

реализация

sys_read

0x03

unsigned int fd

char __user *buf

size_t count

fs/read_write.c:391

sys_write

0x04

unsigned int fd

const char __user *buf

size_t count

fs/read_write.c:408

https://filippo.io/linux-syscall-table/

%rax

Name

Entry point

Implementation

0

read

sys_read

fs/read_write.c

1

write

sys_write

fs/read_write.c

Видим, что соглашения разные (заполняются регистры с разными именами, значения регистров (т.е. номера функций) тоже разные), и про соглашения нужно читать подробнее

http://man7.org/linux/man-pages/man2/syscalls.2.html

the code belonging to the system call with number __NR_xxx
defined in /usr/include/asm/unistd.h can be found in the Linux kernel source in the routine sys_xxx().
(The dispatch table for i386 can be found in /usr/src/linux/arch/i386/kernel/entry.S.)

Тут надо осознать, как устроен репозиторий ядра. Например, [html]<a href="https://github.com/torvalds/linux/tree/master/arch/arm64/include">https://github.com/torvalds/linux/tree/master/arch/arm64/include</a>[/html]в чём разница между директориями asm и uapi-asm ?
https://stackoverflow.com/questions/188 … ce-project

The uapi folder is supposed to contain the user space API of the kernel. Then upon kernel installation, the uapi include files become the top level /usr/include/linux/ files.

[html]<a href="https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/unistd.h">https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/unistd.h</a>[/html]где тут read ?

#define __NR_read 63
__SYSCALL(__NR_read, sys_read)
#define __NR_write 64
__SYSCALL(__NR_write, sys_write)

63 и 64 - это уже третий комплект номеров этих функций.

По вопросу, какие регистры использовать:
https://stackoverflow.com/a/2538212/4158543
советуют прочитать
https://github.com/hjl-tools/x86-psABI/wiki/X86-psABI
(master branch contains x86-64 specification)
[html]
"AMD64 ABI Draft 1.0 – January 28, 2018"<br /><a href="https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf">https://github.com/hjl-tools/x86-psABI/wiki/x86-64-psABI-1.0.pdf</a>[/html]

[html]<a href="https://github.com/torvalds/linux/blob/master/arch/x86/entry/entry_64.S#L126-L136">https://github.com/torvalds/linux/blob/master/arch/x86/entry/entry_64.S#L126-L136</a>[/html]

* Registers on entry:
* rax  system call number
* rcx  return address
* r11  saved rflags (note: r11 is callee-clobbered register in C ABI)
* rdi  arg0
* rsi  arg1
* rdx  arg2
* r10  arg3 (needs to be moved to rcx to conform to C ABI)
* r8   arg4
* r9   arg5
* (note: r12-r15, rbp, rbx are callee-preserved in C ABI)

туториал про сисколлы:
[html]<a href="https://0xax.gitbooks.io/linux-insides/content/SysCall/linux-syscall-1.html">https://0xax.gitbooks.io/linux-insides/content/SysCall/linux-syscall-1.html</a>[/html]
там предлагают пользоваться для определения номеров вот этой таблицей:
[html]<a href="https://github.com/torvalds/linux/blob/master/arch/x86/entry/syscalls/syscall_64.tbl">https://github.com/torvalds/linux/blob/master/arch/x86/entry/syscalls/syscall_64.tbl</a>[/html]

Отредактировано Лис (2019-02-18 06:59:41)