Context switch

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Prathibha Prakash
Posts: 10
Joined: Sat Jun 22, 2019 6:15 pm

Context switch

Post by Prathibha Prakash »

In _port_switch ,why we are using R0 and R1 to store and load the stack pointer(R0 and R1 are general purpose registers).
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Context switch

Post by Giovanni »

Hi,

R0 and R1 are function parameters, see ARM EABI document for details.

Giovanni
Prathibha Prakash
Posts: 10
Joined: Sat Jun 22, 2019 6:15 pm

Re: Context switch

Post by Prathibha Prakash »

Hi

As per the reference viewtopic.php?t=1870#p15421

"The low priority thread would perform its unlock because preempted threads are always in the ISR epilogue code that does exactly that"

Can you point out the code which describes this for more clarification.
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Context switch

Post by Giovanni »

The code is "_port_switch_from_isr" in chcoreasm_v7m.S which calls chSchDoReschedule().

Giovanni
Prathibha Prakash
Posts: 10
Joined: Sat Jun 22, 2019 6:15 pm

Re: Context switch

Post by Prathibha Prakash »

Thank you sir.

1. So the bidirectional arrow from S'-locked to I'-locked state in the system state is meant for this part(preempted threads are always in the ISR epilogue code that does exactly that).

2. Consider main and idle thread.If main sleeps for some some seconds a context switch occurs and the PC contains the next instruction of idle.Now how idle can call unlock??
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Context switch

Post by Giovanni »

The idle thread is always switched out by some IRQ readying other threads.

Giovanni
Prathibha Prakash
Posts: 10
Joined: Sat Jun 22, 2019 6:15 pm

Re: Context switch

Post by Prathibha Prakash »

Can you explain about the the bidirectional arrow from S'-lock to I'-locked state??
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Context switch

Post by Giovanni »

That arrow does not apply to the Cortex/ARM ports, switch always happens in S state.

That transition is possible in those architectures with a single stack where the switch can happen directly in ISRs.

Giovanni
Prathibha Prakash
Posts: 10
Joined: Sat Jun 22, 2019 6:15 pm

Re: Context switch

Post by Prathibha Prakash »

Thank you sir for your quick and informative response.Just one more doubt,hope I'm not bugging you
In the call graph of chSchGosleepS from chibios documentation

http://chibios.sourceforge.net/docs3/rt ... 19c1ccf986

chSyslockFromISR(),chSysUnlockFromISR, chSemFastSignalI, queue_dequeue,chSchReadyI calls are made .
However these calls are not made in the source code (given below for quick reference).
So where these calls are made??

Code: Select all

void chSchGoSleepS(tstate_t newstate) {
  290   thread_t *otp = currp;
  291
  292   chDbgCheckClassS();
  293
  294   /* New state.*/
  295   otp->state = newstate;
  296
  297 #if CH_CFG_TIME_QUANTUM > 0
  298   /* The thread is renouncing its remaining time slices so it will have a new
  299      time quantum when it will wakeup.*/
  300   otp->ticks = (tslices_t)CH_CFG_TIME_QUANTUM;
  301 #endif
  302
  303   /* Next thread in ready list becomes current.*/
  304   currp = queue_fifo_remove(&ch.rlist.queue);
  305   currp->state = CH_STATE_CURRENT;
  306
  307   /* Handling idle-enter hook.*/
  308   if (currp->prio == IDLEPRIO) {
  309     CH_CFG_IDLE_ENTER_HOOK();
  310   }
  311
  312   /* Swap operation as tail call.*/
  313   chSysSwitch(currp, otp);
  314 }
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Context switch

Post by Giovanni »

Doxygen seems to extract wrong information.

Giovanni
Post Reply