2

I'm modifying a scheduler in Minix 3.1.8 and wondering what the system call sys_schedule() does in CPU. Could someone explain?

sys_schedule.c

PUBLIC int sys_schedule(endpoint_t proc_ep, unsigned priority, unsigned quantum)
{
    message m;

    m.SCHEDULING_ENDPOINT = proc_ep;
    m.SCHEDULING_PRIORITY = priority;
    m.SCHEDULING_QUANTUM  = quantum;
    return(_kernel_call(SYS_SCHEDULE, &m));
}

com.h

#define KERNEL_CALL 0x600   /* base for kernel calls to SYSTEM */ 
#  define SYS_SCHEDULE   (KERNEL_CALL + 3)  /* sys_schedule() */

kernel_call.c

PUBLIC int _kernel_call(int syscallnr, message *msgptr)
{
  msgptr->m_type = syscallnr;
  _do_kernel_call(msgptr);
  return(msgptr->m_type);
}

ipc.h

_PROTOTYPE( int _do_kernel_call, (message *m_ptr)           );

_ipc.S

ENTRY(_do_kernel_call)
    /* pass the message pointer to kernel in the %eax register */
    movl    4(%esp), %eax
    int $KERVEC
    ret
Hiroaki Machida
  • 1,060
  • 2
  • 12
  • 23
  • 3
    Are you asking what the code you've shown does, or are you asking what the code that actually implements sys_schedule() does? – Ross Ridge Nov 27 '19 at 08:46
  • Thank you for your comment! I’m asking what the CPU does, such as The CPU runs the thread or CPU put the thread into some queue. – Hiroaki Machida Nov 27 '19 at 09:05
  • Doesn't MINIX have docs for its system calls? Judging from the args, it might be something like POSIX `renice` on yourself, like maybe making your process realtime max priority for a few scheduling timeslices, or possibly it's something like a timed sleep or yield. Those are total guesses based on what you might want an OS to do, and the names of the args. I don't know MINIX at all. – Peter Cordes Nov 27 '19 at 09:25
  • Thank you for your comment! Unfortunately it’s to be documented. https://wiki.minix3.org/doku.php?id=developersguide:kernelapi#sys_schedule Yes, it’s called when nice command is executed. I want to know the exact behavior inside CPU. – Hiroaki Machida Nov 27 '19 at 09:33
  • @HiroakiMachida The CPU reads an instruction from the address stored in the program counter register, and then it increments the program counter register by the number of bytes in the instruction, and then it executes the instruction as specified in the CPU's manual. It repeats this until electrical power is removed. – user253751 Nov 27 '19 at 16:59

2 Answers2

2

Any system call in MINIX will switch to the SYSTEM task (which is what does the code you showed, at least in part). The SYSTEM task has a table which maps the SYS_XXX tags into do_xxx() subroutines. These subroutines are usually contained in small source files in the system/ folder.

There we quickly find do_schedule.c. That file in 3.1.8 is pretty straightforward (and this is clearly explained in the book IIRC), but to give you a resume, it checks its arguments and stores the new scheduling parameters in the calling process table; those new values might then change which process will be picked when the SYSTEM task has ended its job and is about to return to user mode.

AntoineL
  • 888
  • 4
  • 25
2

Minix supports userspace scheduling, which means that a userspace process is responsible for making scheduling decisions for one or more processors. The scheduler processes are invoked by the kernel when there is a need to make such decisions.

The purpose of sys_schedule system call is to enable this userspace scheduling design. A scheduler can invoke sys_schedule to tell the kernel how to schedule a given process. See the documentation page on userspace scheduling.

I've written a description of the SYS_SCHEDULE system call, which you can refer to until the official documentation gets updated.

Parameters:

  • proc_ep: Process endpoint to be reschedule.
  • priority: The priority to assign to the process.
  • quantum: The amount to time to run the process. When the process runs out of quantum, the scheduler assocaited with the process will be informed by the kernel, which can invoke sys_schedule to reschedule the process, thereby putting it in a runnable state again. If the process is not associated with userspace scheduler, the Minix process manager (PM) automatically renews its quantum.

Return value:

  • EINVAL: proc_ep contains an incorrect process number.
  • EPERM: The process that performed the system call is not the scheduler associated with proc_ep, so it doesn't have permission to reschedule the process specified by proc_ep.
  • EINVAL: Invalid priority or quantum.
  • OK: Call succeeded. In this case, the process has been added to the queue associated with the specified priority. The kernel scheduler schedules processes in round-robin fashion starting from the those in the highest-priority queue.

Note that _do_kernel_call doesn't implement sys_schedule, rather it executes int $KERVEC, which is an x86 instruction that performs a user-to-kernel transition and invokes the interrupt handler associated with interrupt number $KERVEC. The interrupt handler then invokes the actual implementation of the system call specified by msgptr->m_type. (See @AntoineL's answer.)

The sys_schedule is used by the PM, which runs in userspace, to balance the priority queues periodically and to automatically renew the quantum of processes that don't have schedulers.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Hadi Brais
  • 22,259
  • 3
  • 54
  • 95