Exodus 22:
According to David, God could have asked Shimei to curse him but turned the curse into a blessing.
A couple years later, before David died, he instructed Solomon in 1 Kings 2:
When he was on the run, he thought that God might have used Shimei to curse him. That didn't mean that Shimei was innocent. Shimei was responsible for what he did. He violated Ex 22:28.
After David's death, Solomon didn't order the execution of Shimei. He offered him a way to stay alive. He ordered him to stay put in Jerusalem. 1 Kings 2:
Was the execution of Shimei justified?
Yes, Shimei agreed to the punishment. He knew what he had done and the consequences.
While David was on the run, Shimei cursed him. Abishai, son of Zeruiah, volunteered to kill him in 2 Samuel 16:28 You shall not revile God, nor curse a ruler of your people.
Shimei and Saul belonged to the tribe of Benjamin.10 But the king said, “What does this have to do with you, you sons of Zeruiah? If he is cursing because the Lord said to him, ‘Curse David,’ who can ask, ‘Why do you do this?’ ” 11 David then said to Abishai and all his officials, “My son, my own flesh and blood, is trying to kill me. How much more, then, this Benjamite! Leave him alone; let him curse, for the Lord has told him to. 12 It may be that the Lord will look upon my misery and restore to me his covenant blessing instead of his curse today.”
According to David, God could have asked Shimei to curse him but turned the curse into a blessing.
A couple years later, before David died, he instructed Solomon in 1 Kings 2:
David did not want Abishai to kill Shimei on the pot. Now, he wanted Solomon to punish Shimei. How to explain this change?8 “And remember, you have with you Shimei son of Gera, the Benjamite from Bahurim, who called down bitter curses on me the day I went to Mahanaim. When he came down to meet me at the Jordan, I swore to him by the Lord: ‘I will not put you to death by the sword.’ 9 But now, do not consider him innocent. You are a man of wisdom; you will know what to do to him. Bring his gray head down to the grave in blood.”
When he was on the run, he thought that God might have used Shimei to curse him. That didn't mean that Shimei was innocent. Shimei was responsible for what he did. He violated Ex 22:28.
After David's death, Solomon didn't order the execution of Shimei. He offered him a way to stay alive. He ordered him to stay put in Jerusalem. 1 Kings 2:
Shimei understood and agreed. But later, he violated the king's command:37 The day you leave and cross the Kidron Valley, you can be sure you will die; your blood will be on your own head.”
Solomon ordered Shimei's execution for what he did to David and for going out of Jerusalem.44 The king also said to Shimei, “You know in your own heart all the harm that you did to David my father. So the LORD will bring back your harm on your own head."
Was the execution of Shimei justified?
Yes, Shimei agreed to the punishment. He knew what he had done and the consequences.